Stuck in an infinite loop

trying to make a script where it waits until an intvalue is a specific number (2), but it seems to just be stuck in an infinite loop.

my script:

Value = workspace.Values.IntO.Value

repeat wait(5) print(".") until Value == 2

print("End")
script.Parent.ClickDetector.MaxActivationDistance = 10

i’ve checked and the value does change, but when it’s 2, it doesn’t do anything. it just keeps looping instead of breaking like i expected it to do.

Value will not be updated due to it just being a number

you could solve this issue by constantly getting the current value by indexing it:

Value = workspace.Values.IntO

if Value.Value ~= 2 then
    repeat wait(5) print(".") until Value.Value == 2
end

print("End")
script.Parent.ClickDetector.MaxActivationDistance = 10
3 Likes

While the above solution provided by @RevokeFriendship will work, Remember that it will mean that it will only update every 5 Seconds and will also be constantly running in the background which could Yield the thread.
A better way around this would be

local Val = workspace.IntO

Val.Changed:Connect(function()
	print("Value Changed!")
	if Val.Value == 2 then
		print("Got it!")
	end
end)
1 Like

Oh and Also, Make sure you are checking for this on the Server or it can easily be exploited!

I wouldn’t use .Changed I would personally do:

local Val = workspace.Int0

if Val.Value ~= 2 then
    repeat
        Val:GetPropertyChangedSignal("Value"):Wait()
    until Val.Value == 2
end

because I assume they want to yield for the change

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.