How can I end the repeat loop using until? I think I am doing something wrong

Hello people. Thank you for reading my post. I have been using loops to check for positions of things and activate certain code based on the position. I have been trying to make a functional elevator by using server script and a click detector.

The following script is a server script (upon click activation) that is intended to open the doors when the elevator cart is on floor 1. If not on floor 1, the script is intended to go on a repeat loop to check for a height of 903 (second floor). If elevator is on height of 903, then it will wait for 10 seconds and if it is still on height of 903 it will close the doors, move down, end the first loop and activate another repeat loop and check for height of 2 (first floor). If the elevator is on height of 2 (first floor), then it will open its doors and end the loop.

Issue
As of now, the majority of the script seems to work beside the first loop (checking for height of 903).
After the second loop, the first loop somehow reactivates again amid the until command. I have been learning about how repeat loops work on the internet but none was able to help me solve my problem.

Sorry for the length of the script, there are two “repeat while” loops after the “else”.

click.MouseClick:Connect(function()
	
		

		rim.BrickColor = BrickColor.new("Lime green")
		UpArrow1.BrickColor = BrickColor.new("Lime green")
		UpArrow2.BrickColor = BrickColor.new("Lime green")
		clickrange.MaxActivationDistance = 0
		print("pass")	

	if math.ceil(determiner1.Position.Y) == 2 then
			
		--	script.Parent.Parent.leftDoor1:SetNetworkOwner(nil)
			--script.Parent.Parent.rightDoor1:SetNetworkOwner(nil)
		
			leftmover.PrismaticConstraint.Velocity = -3
			rightmover.PrismaticConstraint.Velocity = -3
			cartLeftMover.PrismaticConstraint.Velocity = -3
			cartRightMover.PrismaticConstraint.Velocity = -3		
			LobbyLDoor.CanCollide = false
			LobbyRDoor.CanCollide = false
			cartLDoor.CanCollide = false
			cartRDoor.CanCollide = false
			local ElevatorDing = game.ReplicatedStorage["Elevator Sound"]:Clone()
			ElevatorDing.Parent = dingBox
			ElevatorDing.Playing = true
			wait(1)
			rim.BrickColor = BrickColor.new("Really black")
			UpArrow1.BrickColor = BrickColor.new("Really black")
			UpArrow2.BrickColor = BrickColor.new("Really black")
			wait(3.5)
			ElevatorDing:Destroy()
			wait(8)
			clickrange.MaxActivationDistance = 32
	else
		repeat while math.ceil(determiner1.Position.Y) ~= 903 or math.ceil(determiner1.Position.Y) == 903 do
	      wait(0.5) 	
				print("Searching for 903 height")
	 
		if math.ceil(determiner1.Position.Y) == 903  then
			wait(10)
				if math.ceil(determiner1.Position.Y) == 903  then
					print("903 height found")
				clickrange.MaxActivationDistance = 32
				cartRightMover.PrismaticConstraint.UpperLimit = 4.57
				cartLDoor.CanCollide = false
				cartRDoor.CanCollide = false
				SecLeftDoor.CanCollide = false
				SecRightDoor.CanCollide = false
				cartRightMover.PrismaticConstraint.Velocity = 3
				cartLeftMover.PrismaticConstraint.Velocity = 3
				Secleftmover.PrismaticConstraint.Velocity = 3
				Secrightmover.PrismaticConstraint.Velocity = 3
				wait(1.9)
				script.Parent.Parent.roof.Anchored = false
				cartLDoor.CanCollide = true
				cartRDoor.CanCollide = true
				SecLeftDoor.CanCollide = true
				SecRightDoor.CanCollide = true
				wait(3)
				script.Parent.Parent.Lift.PrismaticConstraint.Velocity = 40
				LDownArrow1.BrickColor = BrickColor.new("Really red")
				LDownArrow2.BrickColor = BrickColor.new("Really red")
				SecDownArrow1.BrickColor = BrickColor.new("Really red")
				SecDownArrow2.BrickColor = BrickColor.new("Really red")
						repeat while math.ceil(script.Parent.Parent.Determiner.Position.Y) ~= 2 do
								wait(0.5)
						print("Searching for 2 height")
						if math.ceil(script.Parent.Parent.Determiner.Position.Y) == 2 then
							print("2 height found")
						script.Parent.Parent.Lift.PrismaticConstraint.Velocity = 0
						script.Parent.Parent.ElevatorFloor.Velocity = Vector3.new(script.Parent.Parent.ElevatorFloor.Velocity.X, 0 , script.Parent.Parent.ElevatorFloor.Velocity.Z)
						cartRightMover.PrismaticConstraint.Velocity = -3
						cartLeftMover.PrismaticConstraint.Velocity = -3
						leftmover.PrismaticConstraint.Velocity = -3
						rightmover.PrismaticConstraint.Velocity = -3
						cartLDoor.CanCollide = false
						cartRDoor.CanCollide = false
						LobbyLDoor.CanCollide = false
						LobbyRDoor.CanCollide = false
						wait(0.1)
						rim.BrickColor = BrickColor.new("Really black")
						UpArrow1.BrickColor = BrickColor.new("Really black")
						UpArrow2.BrickColor = BrickColor.new("Really black")
						LDownArrow1.BrickColor = BrickColor.new("Really black")
						LDownArrow2.BrickColor = BrickColor.new("Really black")
						SecDownArrow1.BrickColor = BrickColor.new("Really black")
						SecDownArrow2.BrickColor = BrickColor.new("Really black")
						local ElevatorDing = game.ReplicatedStorage["Elevator Sound"]:Clone()
								ElevatorDing.Parent = dingBox
								ElevatorDing.Playing = true
								wait(3.5)
									ElevatorDing:Destroy()
									end
								end
									until math.ceil(determiner1.Position.Y) == 2 
							end
						end
					end
			
							until  math.ceil(determiner1.Position.Y) == 2 
							end
					

end)

Output
Output1
Thanks for reading this post!

I’m not sure if this will help your problem but to directly answer your question:

-- We want to end the loop when i == 150.

local i = 0

repeat
    wait(0.1) -- Very,very important.
    i = i + 1
until i == 150

Also, I see that you’re using a while loop inside of a repeat loop. So pretty much you just keep creating while loops which can be really expensive.

Try something like this:

repeat
    wait()
until math.ceil(determiner1.Position.Y) ~= 903 or math.ceil(determiner1.Position.Y) == 903

Now, in this case, the do after the until is not needed, but I do sometimes use it to keep everything clean.
Eg,

-- We want to end the loop when i == 150.

local i = 0

repeat
    wait(0.1) -- Very,very important.
    i = i + 1
until i == 150 do
    print("i reached 150!")
end
1 Like

Thanks for tipping me! @suspectshot108 I followed your format and regroup some code like

		repeat
			wait(0.1)
		until math.ceil(determiner1.Position.Y) == 903 do
			wait(10)
           blah blah blah

And it works without any while loops! Thanks!

1 Like