Hunger System problem

So, when I join the game to test it, I set the Value to 1 and when it goes to 0 I die. When I respawn after the death because When Value = 0 then HumHealth = 0. I set the Value to 1 after my death because it got 100 cause it resets when I die and when it reached 0 I didn’t die.

Script:


repeat wait() until Plr.Character

local Hum = Plr.Character:WaitForChild("Humanoid")
local MaxHunger = 100
local DecreaseRate = 3
local HungerValue

if Plr:FindFirstChild("HungerVal") then
	HungerValue = Plr.HungerVal
	HungerValue.Value = MaxHunger
else
	Instance.new("IntValue", Plr).Name = "HungerVal"
	Plr.HungerVal.Value = MaxHunger
	HungerValue = Plr.HungerVal
end

local function updateHungerUI()
	wait()
	script.Parent.Hud.Hunger.UIGradient1.Offset = Vector2.new(0, -Plr.HungerVal.Value / 100)
	wait()
	script.Parent.Hud.Hunger.UIGradient2.Offset = Vector2.new(0, -Plr.HungerVal.Value / 100)
	wait()
	script.Parent.Hud.Hunger.UIGradient3.Offset = Vector2.new(0, -Plr.HungerVal.Value / 100)
	wait(100)
	if Plr.HungerVal.Value > 50 then
		script.Parent.Hud.Hunger.UIGradient1.Enabled = true
		script.Parent.Hud.Hunger.UIGradient2.Enabled = false
		script.Parent.Hud.Hunger.UIGradient3.Enabled = false
	end
	if Plr.HungerVal.Value >= 50 then
		script.Parent.Hud.Hunger.UIGradient1343434.Enabled = true
		script.Parent.Hud.Hunger.UIGradient2334545.Enabled = true
		script.Parent.Hud.Hunger.UIGradient3455454545.Enabled = true
	end
	if Plr.HungerVal.Value >= 25 then
		script.Parent.Hud.Hunger.UIGradient1.Enabled = true
		script.Parent.Hud.Hunger.UIGradient2.Enabled = false
		script.Parent.Hud.Hunger.UIGradient3.Enabled = false
	end
	if Plr.HungerVal.Value = 100 then
		Plr.HungerVal.Value = 100
	end
end

Plr.CharacterAdded:Connect(function(char)
	local humanoid = char:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		Plr.HungerVal.Value = 100
	end)
end)

local function onPlayerDied()
	-- Reset hunger value when the player dies
	if Plr.HungerVal.Value <= 100 then
		Plr.HungerVal.Value = MaxHunger
	end

end

Hum.Died:Connect(onPlayerDied)


HungerValue.Changed:Connect(updateHungerUI)

while wait(DecreaseRate) do
	wait(DecreaseRate)
	HungerValue.Value = HungerValue.Value - 1

	if HungerValue.Value == 0 then
    Hum.Health = 0
	end
end
1 Like

Whenever you respawn (assuming that this script is not in StarterCharacterScripts), the Hum variable is no longer existing, since the character object has been removed, which also removes the Humanoid.

To fix this, you could instead try changing Hum.Health to Plr.Character.Humanoid.Health, since it will get the current character’s humanoid.

Other Issues/Pet Peeves
  1. Use task.wait() instead of wait(), since it is an improved version of the latter.

  2. Instead of using repeat wait() until Plr.Character, you could use local character = Plr.Character or Plr.CharacterAdded:Wait(). This not only waits until the CharacterAdded event has fired to get the character in the case that it isn’t there, it also creates a character variable you could use in your code.

  3. I would recommend using your onPlayerDied function here:

2 Likes

It’s in a local script in StarterGui btw.

Okay, I will try and I will inform you

1 Like

It actually works, thank you so much I had this problem for days and I didn’t know how to fix it, Thank you! Solved!

1 Like

No problem, glad I could help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.