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

How to repeatedly check if statements?

Asked by
Hypgnosis 186
5 years ago

I've written the following code and am having trouble with two if statements (lines 9-14 and lines 16-21). Once hungerValue.Value == (either 50 or 20), the msg.Text does not appear anymore during that play-test.

I need to know how to have the code always be checking the hungerValue.Value, even after the statement has fired.

local hungerArray = {"I'm getting hungry.", "I'm nearly starving."}
local msg = Instance.new("Message", game.Workspace)

while wait(hungerDecrease) do
    if hungerValue.Value - 1 >= 0 then
        hungerValue.Value = hungerValue.Value - 1
    end

    if 
        hungerValue.Value == 50 then
        msg.Text = hungerArray[1]
        wait(5)
        msg:Remove()
    end

    if
        hungerValue.Value == 20 then
        msg.Text = hungerArray[2]
        wait(5)
        msg:Remove()
    end
end
0
dont use wait as a condition in a while loop User#23365 30 — 5y
0
also you could try to connect a event  User#23365 30 — 5y
0
also :Remove() and the parent parameter of Instance.new() is decaperated, instead use :Destroy() and set the parent last User#23365 30 — 5y
0
Not sure if I understand the problem right, but I'm pretty sure you need to move line 02 into the if statements themselves, otherwise msg will be removed and not recreated later...also, wait() is fine as a condition, it does NOT negatively affect readability like some users (literally only on this website alone) seem to believe Vulkarin 581 — 5y
View all comments (2 more)
0
And the Message instance is deprecated. Try switching to a GUI MadWorlds 84 — 5y
0
Thanks for the info, guys. Hypgnosis 186 — 5y

Answer this question