How to Make a Part Slide with CFrame
This tutorial will guide you through the basics of making a Part slide using its CFrame property
Variables
Before we start the actual script, it's useful to have some variables so that we can easily edit the script later. The variables we will use are going to be to control the time it takes to move the Part from point A to point B, the position the Part will be moving to, the Part itself, the distance that the Part will move at one time, and also, a Debounce variable. All of the variables will be made Local, for the sake of efficiency and good practice.
local Part = game.Workspace.Part -- this is the Part we will move local newPos = Part.Position + Vector3.new(0,5,0) -- the position the Part will go to local Time = 10 -- the time that the script will take to move the part local Increment = 0.5 -- the Part will move 0.5 studs each time it moves local Debounce = false
Moving the Part
To move the part, we will want to use a For loop. We will also use the magnitude property to check the distance between the start position and the end position, so we know how many times to move it. First, let's check the magnitude:
local Diff = newPos - Part.Position -- the difference between the two positions local Mag = Diff.magnitude -- the distance between the two parts
The next thing we want to do is find the direction to move the Part. I usually do this with the CFrame's lookVector property, but it can be done with the unit property of a Vector3.
local Direction = CFrame.new(Part.Position, newPos).lookVector
Now, we will create a for loop. The number will start at 0, because the Part will have gone 0 studs, and it will end at Mag, because at the end, Part will have moved by Mag studs. We wil make the number go up by Increment every time the loop runs, because the Part will move Increment studs each time.
for n = 0, Mag, Increment do
And now, we will move the Part towards newPos by adding Direction to its CFrame. Since Direction has a length of 1 stud, just adding Direction to the Part's CFrame will make it move 1 stud. If we multiply Direction by Increment, then it will have the same length as Increment, so we can move it the distance we wanted to.
Part.CFrame = Part.CFrame + (Direction * Increment)
And now, we simply have to wait for a certain amount of time. To get the correct time, we need a little bit of math. We want the entire thing to take 10 seconds. That means that we have to make it so that every second, the script will wait a total of 10/Mag seconds. Since we aren't running this every second, we're running it every Increment seconds, we have to multiply that number by Increment. After that, we just end the loop. Since the function can be triggered at any time, we need to make sure it can only be running once at any given time by using our Debounce variable.
wait( (Time/Mag) * Increment ) end
But if we want this to be triggered by a certain event, then we will have to put it all inside a function. We'll name our function "MovePart".
local Part = game.Workspace.Part -- this is the Part we will move local newPos = Part.Position + Vector3.new(0,5,0) -- the position the Part will go to local Time = 10 -- the time that the script will take to move the part local Increment = 0.5 -- the Part will move 0.5 studs each time it moves local Debounce = false local Diff = newPos - Part.Position -- the difference between the two positions local Mag = Diff.magnitude -- the distance between the two parts local Direction = CFrame.new(Part.Position, newPos).lookVector function MovePart() -- function to move the Part if Debounce then return end -- end the function if debounce is true Debounce = true -- make Debounce true so the function can't run for n = 0, Mag, Increment do Part.CFrame = Part.CFrame + (Direction * Increment) wait( (Time/Mag) * Increment ) end Debounce = false -- set Debounce to false so the function can run again end
And now, we can just hook our function up to an Event by adding a connection. We are going to assume that there is a button in the Workspace with a ClickDetector in it, and that's how the function will be triggered, so here's our connection:
game.Workspace.Button.ClickDetector.MouseClick:connect(MovePart)
Full script
In case you really want to just copy the script that I showed you here, here it is in one complete, working script:
local Part = game.Workspace.Part -- this is the Part we will move local newPos = Part.Position + Vector3.new(0,5,0) -- the position the Part will go to local Time = 10 -- the time that the script will take to move the part local Increment = 0.5 -- the Part will move 0.5 studs each time it moves local Debounce = false local Diff = newPos - Part.Position -- the difference between the two positions local Mag = Diff.magnitude -- the distance between the two parts local Direction = CFrame.new(Part.Position, newPos).lookVector function MovePart() -- function to move the Part if Debounce then return end -- end the function if debounce is true Debounce = true -- make Debounce true so the function can't run for n = 0, Mag, Increment do Part.CFrame = Part.CFrame + (Direction * Increment) wait( (Time/Mag) * Increment ) end Debounce = false -- set Debounce to false so the function can run again end game.Workspace.Button.ClickDetector.MouseClick:connect(MovePart)