Hunger / Thirst System Not Working

Hi I am trying to make a hunger and thirst system where your health goes down when the hunger value reaches 0. But I am not getting any errors and it is not working, any help would be greatly appreciated.

local decreaseFrequency = 2
local decreaseAmount = 30


game.Players.PlayerAdded:Connect(function(player)
	local Thirst = Instance.new("IntValue", player)
	Thirst.Name = "Thirst"
	Thirst.Value = 100
	
	local Hunger = Instance.new("IntValue", player)
	Hunger.Name = "Hunger"
	Hunger.Value = 100
	
	player.CharacterAdded:Connect(function()
		Hunger.Value = 100
		Thirst.Value = 100
	end)
	
	while wait(decreaseFrequency)do
		Hunger.Value -= decreaseAmount
		Thirst.Value -= decreaseAmount
		if Hunger.Value == 0 then
			player.Humanoid.Health -= 2
		end
	end
end)

Try printing around the script to see where its not working (expecially in the while loop).

Also, Humanoid isn’t part of Player, its part of Player.Character

It is the if statement that isn’t working I put a print inside of it, and when the hunger value reached 0 it didn’t print.

Oh, I found out why, try changing Hunger.Value == 0 to Hunger.Value <= 0

Since your decreasing 30 hunger at a time, the hunger value would be -20 not 0.

1 Like
local decreaseFrequency = 2
local decreaseAmount = 30


game.Players.PlayerAdded:Connect(function(player)
	local Thirst = Instance.new("IntValue", player)
	Thirst.Name = "Thirst"
	Thirst.Value = 100
	
	local Hunger = Instance.new("IntValue", player)
	Hunger.Name = "Hunger"
	Hunger.Value = 100
	
	player.CharacterAdded:Connect(function()
		Hunger.Value = 100
		Thirst.Value = 100
	end)
	
	while wait(decreaseFrequency)do
		Hunger.Value -= decreaseAmount
		Thirst.Value -= decreaseAmount
		if Hunger.Value <= 0 then
			player.Character.Humanoid.Health -= 2
		end
	end
end)