Hey. Can anyone in the scripting community tell me how to script a moving part that goes from left to right? I couldn’t find any tutorials on youtube that makes it going from left to right! Thanks
You can do this using tweening if you want and using CFrames. These are two very important skills to learn in scripting.
So first we can set up the tweening:
-- Get the tween service and set it to a variable
local TweenService = game:GetService("TweenService")
-- Set up the part variable
local part = -- Put your object here such as workspace.Part
-- Set up a nil Tween variable to set later:
local Tween
-- Tween Info
local TweenInfo = TweenInfo.new(2) -- It will take 2 seconds to move from left to right.
-- Set up our while loop to make the part move left to right forever.
while true do
wait(2)
Tween = TweenService:Create(part, TweenInfo, {CFrame = part.CFrame * CFrame.new(-5, 0, 0)})
Tween:Play()
-- This will make the part move 5 studs to the left. ^^^
wait(2)
Tween = TweenService:Create(part, TweenInfo, {CFrame = part.CFrame * CFrame.new(5, 0, 0)})
Tween:Play()
-- This will make the part move 5 studs to the right. ^^^
end
make a part, put two part named start and finish and put the script in
local SPEED = 10;
local start = script.Parent:WaitForChild("Start");
local finish = script.Parent:WaitForChild("Finish");
local attach0 = Instance.new("Attachment");
attach0.CFrame = CFrame.new();
attach0.Parent = script.Parent;
local attach1 = Instance.new("Attachment");
attach1.CFrame = finish.CFrame;
attach1.Parent = game.Workspace.Terrain;
local pris = Instance.new("PrismaticConstraint");
pris.ActuatorType = Enum.ActuatorType.Motor;
pris.MotorMaxForce = math.huge;
pris.Attachment0 = attach1;
pris.Attachment1 = attach0;
pris.Parent = script.Parent;
local forward = true;
pris.Velocity = SPEED;
attach1.CFrame = finish.CFrame
start.Touched:Connect(function(hit)
if (hit == script.Parent and not forward) then
forward = true;
pris.Velocity = SPEED;
attach1.CFrame = finish.CFrame
end
end)
finish.Touched:Connect(function(hit)
if (hit == script.Parent and forward) then
forward = false;
pris.Velocity = -SPEED;
attach1.CFrame = start.CFrame
end
end)
it should look like this
(well maybe not the texture)
the part should move where its facing and go the other way wen touched start/finish
3 Likes