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)
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.
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)
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
did i do something wrong again?
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.
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.