I want to create a moving part for my obby game and I can’t find any tutorials to fit my need.
I want my part to be a small square that moves horizontally back and forth a certain distance (the player jumps on when its close to them, and it takes them).
I don’t even know where to begin to start for this, any ideas?
TweenService is the easiest and most optimized method for what you want. It allows you to interpolate properties of an Instance, such as a Part’s position. Here’s an example script that shows how you’d use a Tween to move a Part back and forth:
local part = script.Parent
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
5, --Duration in seconds
Enum.EasingStyle.Linear, --EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime in seconds
)
local tweenGoal = {Position = part.Position + Vector3.new(10,0,0)}
local tween = tweenService:Create(part,tweenInfo,tweenGoal)
tween:Play()
Note the attributes of the TweenInfo. It’s set to: take 5 seconds to completely move the Part; reverse to its original state after moving the part; loop forever. I suggest studying the API page and understanding what Tweens are because they are a very powerful tool.
Its possible to make super complex systems which move the player along with all kinds of surfaces, but if you use robloxs physics system it does this automatically
You could use @Rodtodon’s code, but instead of tweening the “Position” of a part you could tween the “Position” property of a bodymover within an unanchored part as the moving platform
(Make sure you add a bodygyro too with a super high max torque)
You will likely want a VERY big max force (like 9999999999999999999,9999999999999999999,9999999999999999999) to simulate it being anchored
Also make sure you set the bodypositions position property to the initial position of the part beforehand or itll go flying in the first few seconds of the game
(Also mark his code as the solution please that code is a lot harder to write than replacing a few blocks)
Yes, this is correct. I completely forgot that Tweening a position disregards ROBLOX physics, my bad lol. But if OP wanted to use Tweening anyway then they could try to use a PrismaticConstraint | Roblox Creator Documentation to attach the player to the platform.
You can do this by making two Tweens and playing them in 30 second intervals. If you want to interpolate the position of the parts then you can do this:
local part = script.Parent
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
3, --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 tweenUp = tweenService:Create(part,tweenInfo,{Position = part.Position + Vector3.new(0,50,0)})
local tweenDown = tweenService:Create(part,tweenInfo,{Position = part.Position})
tweenUp:Play() --Sends part up
tweenUp.Completed:Connect(function() --When part reaches the top
wait(30)
tweenDown:Play() --Sends part down
tweenDown.Completed:Connect(function() --When part reaches the bottom
wait(30)
tweenUp:Play() --Sends part up
end)
end)
If you want to use @PapaBreadd’s physics-based approach then just make the Tweens change the Position property of a BodyPosition that’s inside the elevator part.