The Button Door
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 20 of 24.
The red button has a ClickDetector like the lamp. Setting Position moves a part instantly. To make it slide, use TweenService: you describe where the part should end up and how long the trip takes, and Roblox animates it.
local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(2)
local goal = { Transparency = 1 }
local fade = TweenService:Create(workspace.Ghost, info, goal)
fade:Play()TweenInfo.new(2) means 2 seconds. The goal is a table of property names and the values to end on. Create makes the tween, and nothing moves until you call Play. A tween can change numbers, Vector3 values like Position and Size, and Color3 colors.
Challenge
MediumA wooden door blocks the gap in the wall, and the red block is a button with a ClickDetector. This script is inside the door.
When the button is clicked, slide the door 10 studs up over 1 second. info and goal are ready: make the tween and play it on MouseClick. The door must not move before the click.
Press Play, walk up to the button, click it and go through. Then Check.
Try it yourself
local TweenService = game:GetService("TweenService")
local door = script.Parent
local button = workspace.Button
local info = TweenInfo.new(1)
local goal = { Position = door.Position + Vector3.new(0, 10, 0) }
-- Make a tween of the door with info and goal
-- Play it when the button's ClickDetector is clicked
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Roblox Game Scripting
2Loops and Functions
Row of PillarsStaircase BuilderTower BuilderCoin FactoryBlinking LightVanishing Bridge5Your First Game
Your First ObbyPractice on your own: Online Lua compiler