Draw Player Health Using Loop

Hey guys.
I’m trying to make a loop that draws player health at the top left corner of the screen as soon as the game loads up starting at 1,0 (x/y coordinates) and ending at 5,0. I already wrote a method in the player script that I call in the game script, but it doesn’t seem to be doing anything. I will try fixing this issue, but for now, does anybody have any ideas for how I could get my drawPlayerHealth method to actually work? Thanks in advance.
Here is the code":

[player script]

on drawPlayerHealth do
barToFill = 0
if playerHealth!=5 then
while playerHealth==0 do
playerHealth++
barToFill++
draw "healthbarFill" at barToFill,0
end
end
end

[game script]

on start do
playerHealth = 0
call "drawPlayerHealth"
end

I’m already working on a solution. Part of the solution is I moved the game script from earlier into the player script:

on drawPlayerHealth do
	barToFill = 0
	drawnHealth = 0
	// if playerHealth!=5 then
	while drawnHealth!=playerHealth do
		drawnHealth++
		barToFill++
		draw "healthbarFill" at barToFill,0
	end
	// end
end

on load do
	playerHealth = 5
	// tell "player" to
	call "drawPlayerHealth"
	// end
end

I would not recommend putting a while loop for any animations. You are trying to animate a growing bar, yes?

If that is the case, the game automatically calls a method named loop once every 1/20 of a second. If you don’t have a method named loop in your game’s script (the one named “game”) you can just added it:

on loop do
  if drawnHealth < playerHealth then
    drawnHealth += 1
  end
end

This will change that variable each game loop until it reaches the player health variable.

Add this to your draw method (or just make this method if you don’t have one.)

on draw do
  i = 0
  while i < drawnHealth do
    draw "healbarFill" at i,0
    i += 1
  end
end

And that will draw how many ever copies of the healbarFill tile as you currently have the drawnHealth variable set to (which will increase one each game loop until it reached playerHealth)

I hope I answered your question, but I’m not totally sure I understood what you were trying to do.