Hunger system script not running

I am making a hunger bar and the script used to work fine, now suddenly it doesn’t run at all. By at all I mean it doesn’t run the loop.

local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait() or player.Character
local humanoid = character:WaitForChild("Humanoid")

local background = script.Parent
local bar = background:WaitForChild("Bar")

local maxHunger = 100
local minHunger = 0
local currentHunger = maxHunger

local timePerLoss = 1
local lossPerTime = 5

while wait(timePerLoss) do
	print("Looping")
	currentHunger -= lossPerTime
	bar.Size = UDim2.new(maxHunger / currentHunger, 0, 1, 0)
	print("Checking")
	if currentHunger <= minHunger then
		bar.Size = UDim2.new(minHunger, 0, 1, 0)
		humanoid.Health = 0
		print("Checked")
		player.CharacterAdded:Connect(function()
			currentHunger = maxHunger
			bar.Size = UDim2.new(1, 0, 1, 0)
			print("Reset")
		end)
	end
end
1 Like

It might be due to 1 of the variables you’ve defined

Are you getting any errors/warnings from the Output at all?

Ok so I was about to reply that I solved the loop issue and got the issue down to 2 lines:
Line 2 and 3.

And no I am not getting errors.

Might be due to this, since the code goes through the lines in a “left to right” fashion, it’ll first wait until a Character has fully loaded or not (Which would probably result as infinite), if that could be the case then can you try replacing lines 2-3 with this?

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
1 Like