How can I fix this hunger system, it goes over 100

local Plr = game.Players.LocalPlayer

repeat wait() until Plr.Character

local Hum = Plr.Character:WaitForChild(“Humanoid”)
local MaxHunger = 100
local DecreaseRate = 40
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

HungerValue.Changed:connect(function()
wait()
script.Parent.Hunger.UIGradient1.Offset = Vector2.new(0,-Plr.HungerVal.Value / 100)
wait()
script.Parent.Hunger.UIGradient2.Offset = Vector2.new(0,-Plr.HungerVal.Value / 100)
wait()
script.Parent.Hunger.UIGradient3.Offset = Vector2.new(0,-Plr.HungerVal.Value / 100)
wait()
if Plr.HungerVal.Value > 50 then
script.Parent.Hunger.UIGradient1.Enabled = true
script.Parent.Hunger.UIGradient2.Enabled = false
script.Parent.Hunger.UIGradient3.Enabled = false
end
if Plr.HungerVal.Value <= 50 then
script.Parent.Hunger.UIGradient1.Enabled = false
script.Parent.Hunger.UIGradient2.Enabled = true
script.Parent.Hunger.UIGradient3.Enabled = false
end
if Plr.HungerVal.Value <= 25 then
script.Parent.Hunger.UIGradient1.Enabled = false
script.Parent.Hunger.UIGradient2.Enabled = false
script.Parent.Hunger.UIGradient3.Enabled = true
end
if Plr.HungerVal.Value >= 100 then
Plr.HungerVal.Value = 100
end
end)

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

if HungerValue.Value == 0 then
	repeat wait(1)
		Hum.Health = Hum.Health - 2
	until HungerValue.Value > 0 or Hum.Health <= 0
end

end

Can someone show me how to fix it? When the Value is 99 and I eat a food it goes over 100 instead of going from 99 to 100 because 100 is the max.

You need to do this on a server script as this won’t replicate to the server, therefore not changing the value. Do this whenever you eat food

You can always use math.clamp(x,y,z) to set a numeral value to a minimum and maximum one.

You are the goat bro thank you so much. It works perfectly.

Thank you too, for trying to help.

Also can you help me with this? It goes over 100 and I used your script, hunger works and it doesnt go over 100 but when I test thirst it goes. That’s the script.

local Plr = game.Players.LocalPlayer

repeat wait() until Plr.Character

local Hum = Plr.Character:WaitForChild(“Humanoid”)
local MaxThirst = 100
local DecreaseRate = 45
local ThirstValue

if Plr:FindFirstChild(“ThirstVal”) then
ThiarstValue = Plr.ThirstVal
ThirstValue.Value = MaxThirst
else
Instance.new(“IntValue”, Plr).Name = “ThirstVal”
Plr.ThirstVal.Value = MaxThirst
ThirstValue = Plr.ThirstVal
end

ThirstValue.Changed:Connect(function()
wait()
script.Parent.Thirst.UIGradient1.Offset = Vector2.new(0,-Plr.ThirstVal.Value / 100)
wait()
script.Parent.Thirst.UIGradient2.Offset = Vector2.new(0,-Plr.ThirstVal.Value / 100)
wait()
script.ParentThirst.UIGradient3.Offset = Vector2.new(0,-Plr.ThirstVal.Value / 100)
wait()
if Plr.ThirstVal.Value > 50 then
script.Parent.Thirst.UIGradient1.Enabled = true
script.Parent.Thirst.UIGradient2.Enabled = false
script.Parent.Thirst.UIGradient3.Enabled = false
end
if Plr.ThirstVal.Value <= 50 then
script.Parent.Thirst.UIGradient1.Enabled = false
script.Parent.Thirst.UIGradient2.Enabled = true
script.Parent.Thirst.UIGradient3.Enabled = false
end
if Plr.ThirstVal.Value <= 25 then
script.Parent.Thirst.UIGradient1.Enabled = false
script.Parent.Thirst.UIGradient2.Enabled = false
script.Parent.Thirst.UIGradient3.Enabled = true
end
if Plr.ThirstVal.Value >= 100 then
Plr.ThirstVal.Value = 100
end
end)

while wait(DecreaseRate) do
if ThirstValue.Value - 1 >= 0 then
ThirstValue.Value = ThirstValue.Value - 1
end

if ThirstValue.Value == 0 then
	repeat wait(1)
		Hum.Health = Hum.Health - 1000
	until ThirstValue.Value > 0 or Hum.Health <= 0
end

end

> Blockquote

If you can tell me what to do, because only hunger works.

Using the same solution (setting thirst value to 100 on the server instead of client) may work. It looks like you are still doing it on the local script

1 Like

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