Script Non-Stop Running

I’m making a theme park and want to open the first section in about 2 months, so I need some help on this script. My ride activates after a button has been pressed, the ride works fine and cycles very well, the problem occurs when the ride stops after the cycle, it just goes again and again forever. I want the ride to cycle once then the button has to be pressed for it to cycle again. I’m assuming the problem occurs because of “when true do” but I’m not for sure. Can somebody tell me what I need to fix in order for this to be solved? I would greatly appreciate it :smiley:

workspace.Tower.Main.PrimaryPart = workspace.Tower.Main.MainPart

local ClickDetector = script.Parent.Parent.balls.ClickDetector
local active = true 
ClickDetector.MouseClick:Connect(function()
	
end)
wait(15)
while active do 
	for i = 1,520 do
		wait(.01)
		
		workspace.Tower.Main:SetPrimaryPartCFrame(CFrame.new(workspace.Tower.Main.PrimaryPart.Position + Vector3.new(0,.5,0)))
	end
	wait(18)
	
	for i = 1,520 do
		wait(.01)

		workspace.Tower.Main:SetPrimaryPartCFrame(CFrame.new(workspace.Tower.Main.PrimaryPart.Position - Vector3.new(0,.5,0)))
	end
	wait(2)
end


It appears you never make the active variable set to false, that is why the ride goes on again and again. Set the active variable to false in the part where the ride is supposed to stop.

Updated Code:

workspace.Tower.Main.PrimaryPart = workspace.Tower.Main.MainPart

local ClickDetector = script.Parent.Parent.balls.ClickDetector
local active = false

ClickDetector.MouseClick:Connect(function()
	active = true
	wait(15)
	while active do 
		for i = 1,520 do
			wait(.01)
		
			workspace.Tower.Main:SetPrimaryPartCFrame(CFrame.new(workspace.Tower.Main.PrimaryPart.Position + Vector3.new(0,.5,0)))
		end
		wait(18)
	
		for i = 1,520 do
			wait(.01)

			workspace.Tower.Main:SetPrimaryPartCFrame(CFrame.new(workspace.Tower.Main.PrimaryPart.Position - Vector3.new(0,.5,0)))
		end
		wait(2)
	
		if active then
			active = false
			break -- breaks the loop
		end
	end
end)
1 Like
workspace.Tower.Main.PrimaryPart = workspace.Tower.Main.MainPart

local ClickDetector = script.Parent.Parent.balls.ClickDetector
local active = true 
ClickDetector.MouseClick:Connect(function()

wait(15)
while active do 
	for i = 1,520 do
		wait(.01)
		
		workspace.Tower.Main:SetPrimaryPartCFrame(CFrame.new(workspace.Tower.Main.PrimaryPart.Position + Vector3.new(0,.5,0)))
	end
	wait(18)
	
	for i = 1,520 do
		wait(.01)

		workspace.Tower.Main:SetPrimaryPartCFrame(CFrame.new(workspace.Tower.Main.PrimaryPart.Position - Vector3.new(0,.5,0)))
	end
	wait(2)
end	
end)

Ayo infinity loop?

so would i just add active = false somewhere

1 Like

Yeah.

Try using this code for your script, see if this works:

workspace.Tower.Main.PrimaryPart = workspace.Tower.Main.MainPart

local ClickDetector = script.Parent.Parent.balls.ClickDetector
local active = false

ClickDetector.MouseClick:Connect(function()
	active = true
	wait(15)
	while active do 
		for i = 1,520 do
			wait(.01)
		
			workspace.Tower.Main:SetPrimaryPartCFrame(CFrame.new(workspace.Tower.Main.PrimaryPart.Position + Vector3.new(0,.5,0)))
		end
		wait(18)
	
		for i = 1,520 do
			wait(.01)

			workspace.Tower.Main:SetPrimaryPartCFrame(CFrame.new(workspace.Tower.Main.PrimaryPart.Position - Vector3.new(0,.5,0)))
		end
		wait(2)
	
		if active then
			active = false
			break -- breaks the loop
		end
	end
end)
1 Like

It works thank u sm but i have a question. whats the difference between

Active = false
and
if active then
active = false
break

1 Like

Well, there is not much of a difference, but I think it’s better to use the if statement to confirm that active is true or false, before breaking the loop. However, since the while loop will only run when the active variable is true, I don’t think that if statement is even necessary. But do make sure to break the loop after making the active variable to false.
Hopefully this is understandable.

And you are welcome, happy to help!

1 Like

Sorry. But one more thing. I have made a few rides but never have I ever made one that spins. So, how can I make the ride go up and spin at the same time? would I use Orientation???

I am not sure about that as I am not experienced in 3D Model Animation. You can make a different topic about this if you are struggling with making a spinning ride.

1 Like