How to Make a Part Slide with CFrame: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
>MrNicNac No edit summary |
>NXTBoy No edit summary |
||
Line 3: | Line 3: | ||
First we define the variables of the code. Use local variables to speed up indexing. | First we define the variables of the code. Use local variables to speed up indexing. | ||
<pre> | <pre> | ||
local Speed = 1 | local Speed = 1 -- Speed of sliding the part. | ||
local Time = 3 | local Time = 3 -- How long the part takes to slide back once moved. | ||
local | 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 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 Part = script.Parent -- The part to slide. | ||
local Movement = Vector3.new(5,0,0) -- Total movement between when door is open and closed | |||
</pre> | </pre> | ||
Line 14: | Line 15: | ||
<pre> | <pre> | ||
function Open() | function Open() | ||
if Running | if not Running then | ||
Running = true | Running = true | ||
for i = 1, | local initialCF = Part.CFrame | ||
wait( | local step = 1/Steps | ||
Part.CFrame = | |||
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 | end | ||
for i = 1, | |||
wait( | for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps. | ||
Part.CFrame = | wait(step) -- This ensures that it'll only wait for 'Speed' seconds. | ||
Part.CFrame = initialCF + Movement*i | |||
end | end | ||
Running = false | Running = false | ||
Line 31: | Line 36: | ||
Now we need to connect a function to the MouseClick event of some [[RBX.lua.ClickDetector_%28Object%29|ClickDetector]]s. 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. | Now we need to connect a function to the MouseClick event of some [[RBX.lua.ClickDetector_%28Object%29|ClickDetector]]s. 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. | ||
<pre> | <pre> | ||
for | for _, child in pairs(script.Parent:GetChildren()) do | ||
if | 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 | ||
end | end | ||
Line 40: | Line 45: | ||
And now, this is what we get if we combine the script: | And now, this is what we get if we combine the script: | ||
<pre> | <pre> | ||
local Speed = 1 | local Speed = 1 -- Speed of sliding the part. | ||
local Time = 3 | local Time = 3 -- How long the part takes to slide back once moved. | ||
local | 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 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 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() | function Open() | ||
if Running | if not Running then | ||
Running = true | Running = true | ||
for i = 1, | local initialCF = Part.CFrame | ||
wait( | local step = 1/Steps | ||
Part.CFrame = | |||
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 | end | ||
for i = 1, | |||
wait( | for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps. | ||
Part.CFrame = | wait(step) -- This ensures that it'll only wait for 'Speed' seconds. | ||
Part.CFrame = initialCF + Movement*i | |||
end | end | ||
Running = false | Running = false | ||
Line 61: | Line 71: | ||
end | end | ||
for | for _, child in pairs(script.Parent:GetChildren()) do | ||
if | 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 | ||
end | end | ||
</pre> | </pre> |
Revision as of 21:22, 13 July 2011
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