Elevator loop equation

Trying to create a loop where start is the current floor and stop is the next floor
I have the for loop because lets say player called the elevator on floor 3 and the elevator is at floor 1,
it goes through multiple floors.
Im struggling with the for i = 1,… and the start and stop variable
The loop should start at the start floor and end at the stop floor…

local dir = 1
if stop < start then dir = -1 end
			
wait(data.Preferences.DoorTime)
			
for i = 1, math.abs(start - stop) do
	local start = start + (i * dir)
	local stop = after - dir

	if i ~= stop then
		local duration = (
			data.Floors[after].PrimaryPart.Position - data.Floors[now].PrimaryPart.Position
		).magnitude / data.Preferences.Speed
					
		wait(duration)
	end
end

I don’t understand the problem in your script. You need to explain more, give screenshots of what you want and what you have!

I want a loop that starts at lets say 10 and goes to 1
It should also do this when you start at 1 and want to go to 10?
Positive and negative incrementing
I tried for i = 10, 1 do and it doesnt do anything

You can use TweenService for this.

You don’t say
But what if the elevator doesn’t just go up, what if it looks like this.


That’s why I need a for loop for each floor in between two floors.

Alright, I think I know what you want.

For this, to increment downwards, you need to do something like this:

for i = 10, 1, -1 do
    -- stuff
end

The third number is the rate at which it increments. You can also do something like this:

for i = 3, 7, 0.5 do
 -- does 8 loops
end

Is there anything else you need?

1 Like

Wow I never knew that I thought a for loop only has a start and a stop and the increment is always 1
Don’t tell anyone shh

2 Likes