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

Why does this part of a health script not work?

Asked by 8 years ago

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

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

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.

Ad
Log in to vote
-1
Answered by 8 years ago

this is not a request site someone moderate this crap

0
This isn't a request. Pyrondon 2089 — 8y

Answer this question