How do i make elevator stop between floors?

I have a part that works as an elevator in my game, its just a platform that moves itself and has no buttons, but i want to it stop for 15 seconds on each floor, how can i do that?

That’s the script

	local part = script.Parent
local tweenService = game:GetService("TweenService")
local tweenInfoDown = TweenInfo.new(
	600, --Duration in seconds
	Enum.EasingStyle.Linear, --EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime in seconds
)
local tweenInfoUp = TweenInfo.new(
	10 , --Duration in seconds
	Enum.EasingStyle.Linear, --EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime in seconds
)
local tweenGoalDown = {Position = part.Position + Vector3.new(0,-1000,0)}
local tweenGoalUp = {Position = part.Position + Vector3.new(0,0,0)}


local tweenDown = tweenService:Create(part,tweenInfoDown,tweenGoalDown)
local tweenUp = tweenService:Create(part,tweenInfoUp,tweenGoalUp)

local canDamage = false
script.Parent.Touched:Connect(function(hit)
	local person = hit.Parent
	local personHumanoid = person and person:FindFirstChild("Humanoid")

	if canDamage and personHumanoid then
		personHumanoid.Health = 0
	end
end)

tweenUp:Play()
tweenDown:Play()
tweenDown.Completed:Connect(function()
	wait(30)
	tweenUp:Play()
	canDamage = true
end)
tweenUp.Completed:Connect(function()
	canDamage = false
	wait(30)
	tweenDown:Play()
end)



tweenDown:Play()
tweenUp:Play()
tweenDown.Completed:Connect(function()wait(30)tweenUp:Play()end)
tweenUp.Completed:Connect(function()wait(30)tweenDown:Play()end)

edit: elevator should stop only when it goes down

Well I would first make an array of int values that represent where you want the elevator to stop on the y-axis. Then, I would make a for loop that tweens the elevator to each goal. For example:

local elevatorLevels = {
   10,
   20,
   40
}

for _, y in pairs(elevatorLevels) do
    local goal = {Position.Y = y}
    -- Tween here using the goal
    task.wait(15)
end

If you want to stop on floors using user input, you can use a dictionary where the key is a ClickDetector and the value is the y position.

2 Likes

So if i have 30 levels do i do it like

local elevatorLevels = {
   1,
   2,
   3,
   4
blabla
}

?
image
Also i get a red here
image

Sorry, I haven’t coded in Studio for a while. Try using Vector 3 values in the dictionary instead and setting the Position rather than Position.Y.

Oh, alright, i tried that


i get that now
image
tick.wait(15) is the 58 line

Did i do something wrong?

	local part = script.Parent
local tweenService = game:GetService("TweenService")
local tweenInfoDown = TweenInfo.new(
	600, --Duration in seconds
	Enum.EasingStyle.Linear, --EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime in seconds
)
local tweenInfoUp = TweenInfo.new(
	10 , --Duration in seconds
	Enum.EasingStyle.Linear, --EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime in seconds
)
local tweenGoalDown = {Position = part.Position + Vector3.new(0,-1000,0)}
local tweenGoalUp = {Position = part.Position + Vector3.new(0,0,0)}


local tweenDown = tweenService:Create(part,tweenInfoDown,tweenGoalDown)
local tweenUp = tweenService:Create(part,tweenInfoUp,tweenGoalUp)

local canDamage = false
script.Parent.Touched:Connect(function(hit)
	local person = hit.Parent
	local personHumanoid = person and person:FindFirstChild("Humanoid")

	if canDamage and personHumanoid then
		personHumanoid.Health = 0
	end
end)

tweenUp:Play()
tweenDown:Play()
tweenDown.Completed:Connect(function()
	wait(30)
	tweenUp:Play()
	canDamage = true
end)
tweenUp.Completed:Connect(function()
	canDamage = false
	wait(30)
	tweenDown:Play()
end)

local elevatorLevels = {
	10,
	20,
	30
}

for _, y in pairs(elevatorLevels) do
	local goal = {Position = Vector3.new(0, -10, 0)} 
	-- Tween here using the goal
	tick.wait(15)
end

tweenDown:Play()
tweenUp:Play()
tweenDown.Completed:Connect(function()wait(30)tweenUp:Play()end)
tweenUp.Completed:Connect(function()wait(30)tweenDown:Play()end)

Agh. Once again, I made a small mistake. It’s task.wait(), not tick.wait(). Sorry about that

O, okay, if i have 30 elevator levels, do i do it like that?
image
or 1 by 1?

P.S Got this now

Do your elevator levels follow a pattern, like each floor is 30 studs below above the previous one? Because in that case, you can just use a for loop and ditch the array instead of using in pairs loop:

for y = 120, 0, -30 do
    local goal = {Position = Vector3.new(10, y, 40)}
end

If your elevator doesn’t follow a pattern, then you have to manually enter y values into the array.

Yea it follows a pattern. So, i dont really understand

	local part = script.Parent
local tweenService = game:GetService("TweenService")
local tweenInfoDown = TweenInfo.new(
	600, --Duration in seconds
	Enum.EasingStyle.Linear, --EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime in seconds
)
local tweenInfoUp = TweenInfo.new(
	10 , --Duration in seconds
	Enum.EasingStyle.Linear, --EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime in seconds
)
local tweenGoalDown = {Position = part.Position + Vector3.new(0,-1000,0)}
local tweenGoalUp = {Position = part.Position + Vector3.new(0,0,0)}


local tweenDown = tweenService:Create(part,tweenInfoDown,tweenGoalDown)
local tweenUp = tweenService:Create(part,tweenInfoUp,tweenGoalUp)

local canDamage = false
script.Parent.Touched:Connect(function(hit)
	local person = hit.Parent
	local personHumanoid = person and person:FindFirstChild("Humanoid")

	if canDamage and personHumanoid then
		personHumanoid.Health = 0
	end
end)

tweenUp:Play()
tweenDown:Play()
tweenDown.Completed:Connect(function()
	wait(30)
	tweenUp:Play()
	canDamage = true
end)
tweenUp.Completed:Connect(function()
	canDamage = false
	wait(30)
	tweenDown:Play()
end)

local elevatorLevels = {
	10,
	20,
	30
}

for y = 120, 0, -30
    local goal = {Position = Vector3.new(10, y, 40)}
	task.wait(15)
end


tweenDown:Play()
tweenUp:Play()
tweenDown.Completed:Connect(function()wait(30)tweenUp:Play()end)
tweenUp.Completed:Connect(function()wait(30)tweenDown:Play()end)

i did that and get that
image
did i do something wrong again?

No, I just forgot to write “do” in front of my for loop. Sorry.

1 Like

image
is there a point to use this still?

I would recommend that you use your own judgement. If you are using a for loop and not using the table, then there shouldn’t be a use for it. If you’re using an in pairs loop and actively using the array, then of course it’s needed.

1 Like

Alright, if theres -15 difference between each floor, what should i do?

Try to break down what the script is doing. I provided a start value (120), an end value (0), and an interval (-30), all of which represent the y value of the elevator. You should change these numbers depending on what you want.
If you want the elevator to go down 15 blocks, then you can change the interval to -15.
If you want the elevator to start going down at 300 studs high, then you can change the start value of the loop to 300.

1 Like

Okay so, i tried 0, -1000, -15, and it never stops on any floor, no errors