How to Make a Part Slide with CFrame
From Legacy Roblox Wiki
This tutorial will guide you through the basics of making a part slide using its CFrame property.
First we define the variables of the code. Use local variables to speed up indexing.
local Speed = 1 -- Speed of sliding the part. local Time = 3 -- How long the part takes to slide back once moved. local Steps = 10 -- How smooth the animation of the part sliding is. The higher, the smoother. local Running = false -- Variable to make sure it can't be opened while it is opening. Basically a 'debounce'. local Part = script.Parent -- The part to slide. local Movement = Vector3.new(5,0,0) -- Total movement between when door is open and closed
We define the function Open() so that we can have multiple events pointing to it. We need to do two loops- One closing, and one opening. Note that we used Debounce.
function Open() if not Running then Running = true local initialCF = Part.CFrame local step = 1/Steps for i = 0, 1, step do --We will run a loop iterating from 0 to 1 in `Steps` steps. wait(step) -- This ensures that it'll only wait for 'Speed' seconds. Part.CFrame = initialCF + Movement*i end for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps. wait(step) -- This ensures that it'll only wait for 'Speed' seconds. Part.CFrame = initialCF + Movement*i end Running = false end end
Now we need to connect a function to the MouseClick event of some ClickDetectors. We are going to loop through the parent model so that we can catch all buttons. Make sure the Part you make that has a ClickDetector in it is named Button for the sake of this tutorial.
for _, child in pairs(script.Parent:GetChildren()) do if child.Name == "Button" then -- Change Button to the name of what needs to be clicked to slide the part. child.ClickDetector.MouseClick:connect(Open) end end
And now, this is what we get if we combine the script:
local Speed = 1 -- Speed of sliding the part. local Time = 3 -- How long the part takes to slide back once moved. local Steps = 10 -- How smooth the animation of the part sliding is. The higher, the smoother. local Running = false -- Variable to make sure it can't be opened while it is opening. Basically a 'debounce'. local Part = script.Parent -- The part to slide. local Movement = Vector3.new(5,0,0) -- Total movement between when door is open and closed function Open() if not Running then Running = true local initialCF = Part.CFrame local step = 1/Steps for i = 0, 1, step do --We will run a loop iterating from 0 to 1 in `Steps` steps. wait(step) -- This ensures that it'll only wait for 'Speed' seconds. Part.CFrame = initialCF + Movement*i end for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps. wait(step) -- This ensures that it'll only wait for 'Speed' seconds. Part.CFrame = initialCF + Movement*i end Running = false end end for _, child in pairs(script.Parent:GetChildren()) do if child.Name == "Button" then -- Change Button to the name of what needs to be clicked to slide the part. child.ClickDetector.MouseClick:connect(Open) end end