Moving Platform
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 22 of 25.
TweenInfo.new takes more than the time. Its values, in order:
- time in seconds
- easing style, such as
Enum.EasingStyle.Sine: how the speed changes along the way - easing direction:
In,OutorInOut - repeat count: how many extra times to play,
-1for forever - reverses:
trueto play backwards after each run - delay: seconds to wait before it starts
local info = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 2, false)This one plays 3 times (once, plus 2 repeats), each run taking 1 second, jumping back to the start between runs.
Sine with InOut starts slowly, speeds up in the middle and slows down at the end, the natural way for a platform to move. Linear keeps one speed the whole way.
A tweened platform does not carry players sideways: they have to walk with it.
Challenge
MediumMake the blue platform glide between Z 10 and Z -10 forever.
- Change only the
TweenInfo.newline. Keep3seconds andSineInOut. - Add a repeat count of
-1(forever) and reversestrue, so it comes back.
Press Play: the platform glides back and forth without stopping. Then Check.
Try it yourself
local TweenService = game:GetService("TweenService")
local platform = script.Parent
-- TweenInfo.new(time, style, direction, repeatCount, reverses)
local info = TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local goal = { Position = Vector3.new(0, 10, -10) }
local tween = TweenService:Create(platform, info, goal)
tween:Play()
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