Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Game stops responding when food and water runs out?

So I got the main part of the script from the usual ROBLOX Health script and added to make sure the stats for water and food is greater than 0.

if Food.Value > 0 and Water.Value > 0 then
		while Humanoid.Health < Humanoid.MaxHealth do
			if Food.Value > 0 and Water.Value > 0 then
				local E = wait(1)
				local Health = Humanoid.Health
				local MaxHealth = Humanoid.MaxHealth
				if Health > 0 and Health < MaxHealth then
					local AddedHealth = 0.01 * E * MaxHealth
					Health = Health + AddedHealth
					Humanoid.Health = math.min(Health, Humanoid.MaxHealth)
				end
			end
		end
		if Humanoid.Health > Humanoid.MaxHealth then
			Humanoid.Health = Humanoid.MaxHealth
		end
		Regening = false
	end

When I test it and the food and water runs out, the game stops responding. Any ideas?

3 comments

0

Add an wait() function.

0

Where?

0

Add a wait below line 11, whereas the while ends before it

2 answers

✓ Accepted answer

Your wait() is only effective if your food and water are at more than 0. Just go ahead and put a wait() right after your loop is called.

The wait() function is very important when using a loop, This prevents game crashes. You can place you wait() function in your loop or next to the condition, example. while condition and wait() do return true end or while true do; wait(); end

This discussion is preserved as it was. New answers, comments, and votes are closed.