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

Why does my food system work once?

Alright I'm implementing a food system into my game and it works fine, but when I die it just stops updating the GUI I have set up. Please help.

Code:

wait(1)
local player = game.Players.LocalPlayer
local hungerLevels = {full = true, kindof = false, hungry = false, starving = false, dying = false, dead = false}

local function check()
	if hungerLevels.dead then
		player.PlayerGui.MainUI.ScreenGui.Frame.HungerInteger.Text = "Dead"
		local calculateDamage = 100 - player.Character.Humanoid.Health
		player.Character.Humanoid:TakeDamage(calculateDamage)
		else if hungerLevels.dying then
			player.PlayerGui.MainUI.ScreenGui.Frame.HungerInteger.Text = "Dying"
			local message = Instance.new("Message", game.Workspace)
			message.Text = "YOU ARE DYING OF STARVATION! GO EAT!!!"
			wait(3)
			message:Destroy()
			else if hungerLevels.starving then
				player.PlayerGui.MainUI.ScreenGui.Frame.HungerInteger.Text = "Starving"
				else if hungerLevels.hungry then
					player.PlayerGui.MainUI.ScreenGui.Frame.HungerInteger.Text = "Hungry"
					else if hungerLevels.kindof then
						player.PlayerGui.MainUI.ScreenGui.Frame.HungerInteger.Text = "Okay"	
						else if hungerLevels.full then				
							player.PlayerGui.MainUI.ScreenGui.Frame.HungerInteger.Text = "Full"						
						end					
					end				
				end
			end
		end
	end
end

local function starvationSystem()
	check()
	wait(60)
	hungerLevels.full = false
	hungerLevels.kindof = true
	check()
	wait(60)
	hungerLevels.kindof = false
	hungerLevels.hungry = true
	check()
	wait(60 * 2)
	hungerLevels.hungry = false
	hungerLevels.starving = true
	check()
	wait(60 * 2)
	hungerLevels.starving = false
	hungerLevels.dying = true
	check()
	wait(60 * 3)
	hungerLevels.dying = false
	hungerLevels.dead = true
	check()
end

while player.Character.Humanoid.Health > 0 do
	starvationSystem()
end

1 answer

while player.Character.Humanoid.Health > 0 do
    starvationSystem()
end

Of course it stops updating. That line of code is saying; "while the localplayer's health is above 0, run the starvationsystem."

1 comment

0

I'd agree to this answer, as from what i knew, Loops will only work if the statement if true, and stops and continues to the end of script when the loop ends, So maybe, attach some Humanoid Events? on Respawn, to Re-declare the loop.

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