Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

why will my if statement not work???

Asked by 8 years ago

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)

0
first of all, when you have two while loops in the same script, the seond one isn't going to do anything theCJarmy7 1293 — 8y
0
well it does coolblockman22 5 — 8y
0
Lol, yeah it would still work :P But I don't think that's the best way of doing it :P User#11440 120 — 8y

1 answer

Log in to vote
0
Answered by
jellp 0
8 years ago

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)


Ad

Answer this question