Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this part of a health script not work?

SO I am trying to make a hunger and thirst script, and the if statement where I see if the hunger and thirst is below 1 is not seeming to work. Can anybody look at it and see what my problem is. It is not giving me any errors in the output.

function onPlayerEnter(player)
	local playername = player.Name
	print(playername)
	local humanoid = game.Workspace:FindFirstChild(playername)
	
	local hunger = Instance.new("NumberValue") -- Creating the hunger value
	hunger.Name = "Hunger"
	hunger.Parent = player
	hunger.Value = 100
	
	local thirst = Instance.new("NumberValue") -- Creating the thirst value
	thirst.Name = "Thirst"
	thirst.Parent = player
	thirst.Value = 100
	
	while true do
		wait(0.1)
		hunger.Value = hunger.Value - 1
		thirst.Value = thirst.Value - 1
	end

		if hunger.Value < 1 or thirst.Value < 1 then -- Part that is not seeming to work
			while true do
				wait(1)
				humanoid.Health = humanoid.Health - 1
			end
		end
end

game.Players.ChildAdded:connect(onPlayerEnter)

2 answers

✓ Accepted answer

The reason it isn't getting to the if statement is because the script is still running the loop, and never checks the if statement.

Either way, the check would only run once. You need to connect both values to a .Changed event.

function onPlayerEnter(player)
    local playername = player.Name
    print(playername)
    local humanoid = game.Workspace:FindFirstChild(playername)

    local hunger = Instance.new("NumberValue") -- Creating the hunger value
    hunger.Name = "Hunger"
    hunger.Parent = player
    hunger.Value = 100

    local thirst = Instance.new("NumberValue") -- Creating the thirst value
    thirst.Name = "Thirst"
    thirst.Parent = player
    thirst.Value = 100

	function check()
		if hunger.Value < 1 or thirst.Value < 1 then -- Part that is not seeming to work
			while (hunger.Value < 1 or thirst.Value < 1) do -- Added this in, as I assume you don't want the health to decrease forever.
				wait(1)
				humanoid.Health = humanoid.Health - 1
			end
		end
	end

    while true do
        wait(0.1)
        hunger.Value = hunger.Value - 1
        thirst.Value = thirst.Value - 1
    end

game.Players.ChildAdded:connect(onPlayerEnter)
hunger.Changed:connect(check)
thirst.Changed:connect(check)

If this doesn't work, try separating them into two scripts; one for checking and one for decreasing. I think this should work, though. Hope this helped.

this is not a request site someone moderate this crap

1 comment

0

This isn't a request.

This discussion is preserved as it was. New answers, comments, and votes are closed.