Roblox Studio · Lesson 12

Filling animated: production of smooth lift doors

Use TweenService, TweenInfo and Completed to make a door that can be opened, waited for, and closed.

10–15 years old 60 minutes TweenService Hands-on practice Testing and Saving

Course overview

Let the base gate slowly rise and again stabilize back to its place.

Modifying Position directly will cause the door to jump to the new position instantly. TweenService is responsible for calculating the intermediate process, allowing you to specify "from here to there, complete in one second."

We first use touch to open the door, and then switch to active buttons in the next lesson. The door is just a visual and collision exercise and does not carry passengers, nor does it serve as a safety permission boundary.

01 · BEFORE

Before starting

Touched and busy will be used. Create a new Lesson12 and learn that Position is a Vector3.

02 · MAKE

Your work

After touching the switch, the door is raised for one second, maintained for two seconds and dropped for one second, during which it will not be activated again.

03 · CHECK

Complete flag

The switch will not shake; each time the door closes, it returns to the same position and does not rise higher.

Official easing curve comparison chart, not a screenshot of the door's operation
Figure 12-1: Official easing curve: the curve shows the speed of change, not the operation result of the door. Use Sine to make a smooth move first, then compare to other curves.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Learning route

Prepare first, then do it; after seeing the result, remember to save it.

Every time a small step is completed, it is tested. Say what you expect to see first, and then use Play and Output to check it. Don't rush to write all the code at once.

1

01 · Open the door and switch separately

The door should be Anchored=true, so gravity does not compete to control it. Collapse closes during the opening and closes for a while, avoiding animating the character.

2

02· A tween requires three pieces of information

TweenService:Create (object, TweenInfo, target table) You need to know who to change, how to change, and what to change. Keys in the target table must be real property names, such as Position.

3

03· Wait for the animation to be completed before executing the next step

Completed:Wait() Wait for this animation to end before starting to maintain or close. It's clearer than randomly guessing the duration of a task.wait.

4

04 · Change only one animation feel at a time

Change the duration from 1 to 2 and the door will be slower. Change Sine to Linear to make the change speed more even. Only change one item at a time and you will know where the difference comes from.

5

05 · Check repeat trigger and last position

It is not enough to open the door once and look good. It should also return to the correct position after five consecutive runs. The position is getting more and more offset, usually indicating that the starting point or end point is re-accumulated during the cycle.

Step by step

01 · Open the door and switch separately

The door should be Anchored=true, so gravity does not compete to control it. Collapse closes during the opening and closes for a while, avoiding animating the character.

Do not stand in the final closing position when practicing. The real door also needs to detect whether there is someone at the door. In this lesson, we will first make the complete animation cycle clear.

  1. Create Gate: Size=10, 10, 1, Position=0, 5, -16, Anchored=true.
  2. Create Switch: Size=6, 1, 6, Position=0, 0.5, -8, Anchored=true, CanTouch=true.
  3. Create a common Script in Switch: DoorTween.

Step by step

02· A tween requires three pieces of information

TweenService:Create (object, TweenInfo, target table) You need to know who to change, how to change, and what to change. Keys in the target table must be real property names, such as Position.

TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) means that it takes one second to complete, and the start and end are relatively soft. Create only prepares the animation, and Play will start.

  1. Find the two Creates in the complete script and compare their target Positions.
  2. closedPosition is only recorded once at the beginning to avoid treating "current position plus 10" as the end point every time it is opened.
Use Output to check the screenshot of the script feedback interface
Figure 12-2: Official Output interface reference: When the door does not move, first check the error message and script line number, and then confirm the Gate and Switch names.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Step by step

03· Wait for the animation to be completed before executing the next step

Completed:Wait() Wait for this animation to end before starting to maintain or close. It's clearer than randomly guessing the duration of a task.wait.

busy is set to true before any wait and is restored after the entire switching cycle. Another player stepping on the switch at this time will not create a second set of competing animations.

  1. Place the complete code into Switch > DoorTween.
  2. Step on the switch after playing and observe the complete switching process.
  3. The door should remain for only one cycle, either on a continuous basis or with the two players.
Workspace > Switch > DoorTween · Complete scriptLuau
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local switch = script.Parent
local gate = workspace:WaitForChild("Gate")
local closedPosition = gate.Position
local openPosition = closedPosition + Vector3.new(0, 10, 0)
local info = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local busy = false

switch.Touched:Connect(function(hit)
    if busy then return end
    local character = hit:FindFirstAncestorOfClass("Model")
    local player = character and Players:GetPlayerFromCharacter(character)
    local humanoid = character and character:FindFirstChildOfClass("Humanoid")
    if not player or not humanoid or humanoid.Health <= 0 then return end
    busy = true
    gate.CanCollide = false
    local opening = TweenService:Create(gate, info, {Position = openPosition})
    opening:Play()
    opening.Completed:Wait()
    task.wait(2)
    local closing = TweenService:Create(gate, info, {Position = closedPosition})
    closing:Play()
    closing.Completed:Wait()
    gate.CanCollide = true
    busy = false
end)

Step by step

04 · Change only one animation feel at a time

Change the duration from 1 to 2 and the door will be slower. Change Sine to Linear to make the change speed more even. Only change one item at a time and you will know where the difference comes from.

Tweens running simultaneously on the same property will affect each other. Don't keep assigning values ​​to Gate.Position in another script.

  1. Compare the door opening process of one second and two seconds.
  2. Change the rising distance to 12 and confirm that it does not touch the roof.
  3. Keep the version that is easiest to observe without pursuing exaggerated bounce.

Step by step

05 · Check repeat trigger and last position

It is not enough to open the door once and look good. It should also return to the correct position after five consecutive runs. The position is getting more and more offset, usually indicating that the starting point or end point is re-accumulated during the cycle.

Tween moving Anchored platforms are not guaranteed to move the character as reliably as an elevator. Making a manned platform requires additional research on physics and character behavior, so I won’t copy it directly here.

  1. Operation of five rounds, each end of inspection Gate.Position.
  2. CanCollide returns to true after confirming the door is closed.
  3. Save your three parameters: starting point, end point and animation duration.

Homework

It's your turn to change and make your own version.

1 Slow warehouse door

Change the switch animation to two seconds and the holding time to three seconds.

2 Colour Matching

Turn the switch orange when busy and return to blue when finished.

3 Selection: Side slide door

Change the end point offset to the X direction, making sure there is enough space on the sides.

Completion criteria:First verify the work in Playtest, then click Stop and save it as "Initials_Lesson12" or a copy of the corresponding Homework; do not overwrite the runnable version of the previous lesson.

Exit questions

Answer it yourself first, and then expand on it.

Why hasn’t the Create backdoor moved yet?

Also needs to call Play on the generated Tween.

Why is closedPosition recorded only once?

Avoiding each opening of the door from adding up to a further distance from the coordinates after the change, leading to a drift of position.

When can busy be restored?

After opening the door, holding the door, and closing the door are all completed.

Is this an example of a human elevator?

It should not be copied directly, moving tweens and reliably transporting characters are not the same problem.

Explore further

This is where we start when we need to check the data.

The text code can be copied directly. First confirm the script type, placement, and whether this code is a complete version or a small experiment; do not run multiple versions of the same script superimposed. The source of the interface reference picture has been marked. The object names in the picture may be different from those in this lesson. Please refer to the operation steps.

Download the code and placement instructions for this lesson