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

How to make a while (value) true loop run again after being falsified?

I'm trying to make a script that resets this loop after the 'voting' value is turned back to true. 'Voting' is a boolean value and 'votetime' is an IntValue.

voting = script.Parent.Parent.voting
votetime = script.Parent

while voting.Value == true do wait(1)
	votetime.Value = votetime.Value - 1
end

I tried using the .Changed event but I can't figure out how to make it work with the boolean (attempt to index field 'value', a boolean value). Any idea on how to make this loop run again after the voting value is changed to false than true again? I'd also like to avoid constantly running a loop to check. Thanks.

1 answer

✓ Accepted answer

I tried this myself and added a variable to know if it is already counting. When it is changed, I call a function that will make sure the value is true and there is not already a script counting and will start counting.

local voting = script.Parent.Parent.voting
local votetime = script.Parent
local Counting = false --Is there a loop counting?

function BeginCounting()
	if not Counting then
		Counting = true
		
		while voting.Value == true do 
		    wait(1)
		    votetime.Value = votetime.Value - 1
		end
		
		Counting = false
	end
end

spawn(function()
	BeginCounting()
end)

voting.Changed:Connect(function()
	if voting.Value and not Counting then --If it's true and there's not a current loop
		BeginCounting()
	end
end)

1 comment

0

Worked just how I wanted, thanks!

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