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

why will my if statement not work???

I am trying to get the hunger and thirst to regen after the character dies and this is the only method I can think of to make this work.

function onPlayerEnter(player)
	
	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(2)
		hunger.Value = hunger.Value - 50
		thirst.Value = thirst.Value - 50
		
		if player.Character.Humanoid.Health <= 0 then -- Part that is not seeming to work. ):
			print("Working")
			wait(6)
			hunger.Value = 100
			thirst.Value = 100
		end
		
		while hunger.Value < 0 or thirst.Value < 0 do
			wait(0.5)
			player.Character.Humanoid.Health = player.Character.Humanoid.Health - 5
		end
	end
end

game.Players.ChildAdded:connect(onPlayerEnter)

3 comments

0

first of all, when you have two while loops in the same script, the seond one isn't going to do anything

0

well it does

0

Lol, yeah it would still work :P But I don't think that's the best way of doing it :P

1 answer

This is a bit of a workaround, but I'd put the Hunger and Thirst stat inside of the character model and put it in the character every time he respawns.

Also, this is my first post around here, so hi everyone. ^-^

Anyway, it would result in this code:

function onPlayerEnter(player)
    player.CharacterAdded:connect(function(character)
        local hunger = Instance.new("NumberValue") -- Creating the hunger value
        hunger.Name = "Hunger"
        hunger.Parent = player.Character
        hunger.Value = 100

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

        while true do
            wait(2)
            hunger.Value = hunger.Value - 50
            thirst.Value = thirst.Value - 50

           --We don't require this anymore, player.CharacterAdded is also fired on respawn.
--            if player.Character.Humanoid.Health <= 0 then -- Part that is not seeming to work. ):
--                print("Working")
--                wait(6)
--                hunger.Value = 100
--                thirst.Value = 100
--            end

            while hunger.Value < 0 or thirst.Value < 0 do
                wait(0.5)
                player.Character.Humanoid.Health = player.Character.Humanoid.Health - 5
            end
        end
    end)
end

game.Players.ChildAdded:connect(onPlayerEnter)


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