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

Repeat until loop doesn't stop when bool value is true?

In my flood script a part goes towards land like a flood but it doesn't stop when I want it to. It does all the other things, but the loop doesn't stop. What is the problem I am having?

Code:

local flood = game.ReplicatedStorage.Values.Flood
local stop = false

flood.Changed:Connect(function()
	script.Parent.Transparency = 0.3
	if flood.Value == false then
		stop = true
	end
	repeat --This loop doesn't stop
		script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,0,-1)
		wait(0.01)
	until stop == true
	
	stop = false
	script.Parent.Position = Vector3.new(-926.5, -21.25, 483.375)
	script.Parent.Size = Vector3.new(339.5, 9, 616.25)
	script.Parent.Transparency = 0
end)

Thanks!

2 answers

✓ Accepted answer

The issue is you started a loop that's essentially infinite, since the value it depends on can only be changed outside of the loop. Since we need to change that value to exit the loop, we add the if statement to it.

Although im not totally sure why we're changing a bool with a bool, instead of just checking floods value?

local flood = game.ReplicatedStorage.Values.Flood
local stop = false

flood.Changed:Connect(function()
	script.Parent.Transparency = 0.3

	while not stop do
		script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,0,-1)
		wait(0.01)
		if flood.Value == false then
			stop = true
		end
	end

	stop = false
	script.Parent.Position = Vector3.new(-926.5, -21.25, 483.375)
	script.Parent.Size = Vector3.new(339.5, 9, 616.25)
	script.Parent.Transparency = 0
end)

The problem is you're looping through the repeat loop. However the stop = false is never reached because of the loop. I would imagine you could just change

until stop == true

to

until script.Parent.CFrame = <whatever CFrame you want>

1 comment

0

I don't want it to stop at a certain CFrame because sometime flood goes more distance and sometime less distance.

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