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

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

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!

4 comments

0

you can try connecting a event

0

You could used :Changed() to make if the string value of the player's hunger is an instance.

0

Changed isnt a function, its a event

0

can you show use the complete script?

1 answer

✓ Accepted answer

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)

1 comment

0

The spawn(function() worked perfectly. Thanks for the explanation.

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