Why does my hunger and thirst script work only once?

Hey, I’m trying to make a hunger and thirst script, it works fine until you reset or just die from not eating or drinking.

When the character respawns the hunger and thirst keeps getting lower as it should, but it does not damage the player when the hunger or thirst is under 1?

Does anyone know what’s the problem here? What I know the while true do loop is still running but the "human.Health = human.Health -22" etc is not longer working.

game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Parent = player
	folder.Name = "Hunger&Thirst"
	
	local hunger = Instance.new("IntValue")
	hunger.Parent = folder
	hunger.Name = "Hunger"
	
	hunger.Value = 100
	
	local thirst = Instance.new("IntValue")
	thirst.Parent = folder
	thirst.Name = "Thirst"
	
	thirst.Value = 100
	
	player.CharacterAdded:Connect(function()
		hunger.Value = 100
		thirst.Value = 100	
	end)
	
local char = player.CharacterAdded:Wait()	
local human = char:FindFirstChild("Humanoid")
	
	while true do
		wait(5)
		
		if hunger.Value > 0 then
			hunger.Value = hunger.Value -1
		end
		
		if thirst.Value > 0 then
		thirst.Value = thirst.Value -2
		end
		
		if hunger.Value < 1 then
			human.Health = human.Health -22
		end
		
		if thirst.Value < 1 then
			human.Health = human.Health -28
		end	
	end
end)

Thanks for the help, if you have any questings please ask me :slight_smile:
/Xsodar

After character dies, these variables doesn’t refer to the current character but the old character and its humanoid. You can try rearranging the code:

	local char
	local human
	player.CharacterAdded:Connect(function(character)
		hunger.Value = 100
		thirst.Value = 100
		
		char = character
		human = character:FindFirstChild("Humanoid")
	end)
1 Like

Thank you very much, now I know!