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

How can I constantly check if statements in a while wait(10) loop?

Asked by
Hypgnosis 186
5 years ago

Sorry if the title is confusing; I'll explain better here.

In the below code, the program is waiting 10 seconds, subtracting from an IntValue (hungerValue.Value), then checking that IntValue. If the value == 60, 40, or 20, it displays one of three messages from an array.

I also want have this hungerValue decrease when the player jumps, but the code only checks for the hungerValue.Value after every 'wait(10).' Meaning, the messages only appear if the hungerValue ticks down. If a player 'jumps' his/her hungerValue down to 60,40, or 20, the gui doesn't appear.

So, my underlying question is: how can I check those if statements at every moment, so that if the player 'jumps' his/her hungerValue down to 60, 40, or 20, the gui appears.

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

    if 
        hungerValue.Value == 60 then
        HungerFrame.TextLabel.Text = hungerStrings[1]
        HungerFrame.Visible = true
        wait(5)
        HungerFrame.Visible = false
    end

    if
        hungerValue.Value == 40 then
        HungerFrame.TextLabel.Text = hungerStrings[2]
        HungerFrame.Visible = true
        wait(5)
        HungerFrame.Visible = false
    end

    if
        hungerValue.Value == 20 then
        HungerFrame.TextLabel.Text = hungerStrings[3]
        HungerFrame.Visible = true
        wait(5)
        HungerFrame.Visible = false
    end

Any help or hints in the right direction would be greatly appreciated!

0
you can try connecting a event User#23365 30 — 5y
0
You could used :Changed() to make if the string value of the player's hunger is an instance. Jexpler 63 — 5y
0
Changed isnt a function, its a event User#23365 30 — 5y
0
can you show use the complete script? User#23793 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

two ways you can check if the value changed:

hungerValue.Value.Changed:Connect(function()
if
            hungerValue.Value == 60 then
            HungerFrame.TextLabel.Text = hungerStrings[1]
            HungerFrame.Visible = true
            wait(5)
            HungerFrame.Visible = false
        end

        if
            hungerValue.Value == 40 then
            HungerFrame.TextLabel.Text = hungerStrings[2]
            HungerFrame.Visible = true
            wait(5)
            HungerFrame.Visible = false
        end

        if
            hungerValue.Value == 20 then
            HungerFrame.TextLabel.Text = hungerStrings[3]
            HungerFrame.Visible = true
            wait(5)
            HungerFrame.Visible = false
        end
end)

or you can constantly check it by using spawn functions they are code that runs but the script keeps them going while the rest of the script continues

spawn(function()
while true do
wait()
if
            hungerValue.Value == 60 then
            HungerFrame.TextLabel.Text = hungerStrings[1]
            HungerFrame.Visible = true
            wait(5)
            HungerFrame.Visible = false
        end

        if
            hungerValue.Value == 40 then
            HungerFrame.TextLabel.Text = hungerStrings[2]
            HungerFrame.Visible = true
            wait(5)
            HungerFrame.Visible = false
        end

        if
            hungerValue.Value == 20 then
            HungerFrame.TextLabel.Text = hungerStrings[3]
            HungerFrame.Visible = true
            wait(5)
            HungerFrame.Visible = false
        end
end
end)
0
The spawn(function() worked perfectly. Thanks for the explanation. Hypgnosis 186 — 5y
Ad

Answer this question