Hello Developers!
Im making a game where if you ran out of hunger you will reduce health. But what happen is it reduce my health while not out of hunger. Script i created:
while wait(1) do
if script.Parent.Value ~= 0 then
script.Parent.Value = script.Parent.Value - 0.1
elseif script.Parent.Value <= 0 then
script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health - 1
end
end
I assume the script is a child of the Hunger ValueObject? Assuming this, you wrote your conditional wrong.
~= means “Not”
What you need is: == which means, “Is” or “Is Equal to”
Your script should look like this instead:
while wait(1) do
if script.Parent.Value == 0 then
script.Parent.Value = script.Parent.Value - 0.1
else
script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health - 1
end
end
This way you also don’t need the “elseif” because when Hunger is anything other than 0, you don’t want to be losing health. If it’s possible that Hunger can go below 0, then you can set the conditional to be:
Ohhh my bad I didn’t look at it thoroughly enough.
It might honestly be the indention. I don’t think Lua cares about formatting like that, but it may. Try moving the elseif back by one indent so that it lines up with the if. Like this:
while wait(1) do
if script.Parent.Value ~= 0 then
script.Parent.Value = script.Parent.Value - 0.1
elseif script.Parent.Value <= 0 then
script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health - 1
end
end
Wait! it suddenly fixed my script! i didn’t even change anything at the script except the math.clamp and others! imma solution you since you gave me more knowledge in lua with math.clamp. Thank you!