How to Make a Part Slide with CFrame: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>NXTBoy
No edit summary
>NXTBoy
No edit summary
Line 1: Line 1:
This tutorial will guide you through the basics of making a part slide using its CFrame property.
This tutorial will guide you through the basics of making a part slide using its CFrame property.
==Setting the variables==


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>
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
</pre>


local timing = {
    opening = 1, -- Time the door takes to open.
    open = 3,    -- Time the door stays open
    closing = 1  -- Time the door takes to close
}
local steps = 10 -- How smooth the animation of the part sliding is. The higher, the smoother.
local closed = true -- 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
== Opening the door ==


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]].
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]].
<pre>
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.
function Open()
         wait(step) -- This ensures that it'll only wait for 'Speed' seconds.
    if closed then
        Part.CFrame = initialCF + Movement*i
        closed = false
      end
        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 * timing.opening)
            part.CFrame = initialCF + movement*i
        end
       
         wait(timing.open)
       
        for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps.
            wait(step * timing.closing)
            part.CFrame = initialCF + movement*i
        end
        closed = true
    end
end


      for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps.
== Binding it to a MouseClick event ==
        wait(step) -- This ensures that it'll only wait for 'Speed' seconds.
        Part.CFrame = initialCF + Movement*i
      end
      Running = false
  end
end
</pre>


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>
 
for _, child in pairs(script.Parent:GetChildren()) do
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.
    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)
        child.ClickDetector.MouseClick:connect(Open)
  end
    end
end
end
</pre>
 
== Complete script ==


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>
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.
local timing = {
        wait(step) -- This ensures that it'll only wait for 'Speed' seconds.
    opening = 1, -- Time the door takes to open.
        Part.CFrame = initialCF + Movement*i
    open = 3,    -- Time the door stays open
      end
    closing = 1  -- Time the door takes to close
}
         
local steps = 10 -- How smooth the animation of the part sliding is. The higher, the smoother.


      for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps.
local closed = true -- Variable to make sure it can't be opened while it is opening. Basically a 'debounce'.
        wait(step) -- This ensures that it'll only wait for 'Speed' seconds.
local part = script.Parent          -- The part to slide.
        Part.CFrame = initialCF + Movement*i
local movement = Vector3.new(5, 0, 0) -- Total movement between when door is open and closed
      end
      Running = false
function Open()
  end
    if closed then
end
        closed = false
        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 * timing.opening)
            part.CFrame = initialCF + movement*i
        end
       
        wait(timing.open)


for _, child in pairs(script.Parent:GetChildren()) do
        for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps.
  if child.Name == "Button" then -- Change Button to the name of what needs to be clicked to slide the part.
            wait(step * timing.closing)
      child.ClickDetector.MouseClick:connect(Open)
            part.CFrame = initialCF + movement*i
  end
        end
end
        closed = true
</pre>
    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

Revision as of 16:20, 24 July 2011

This tutorial will guide you through the basics of making a part slide using its CFrame property.

Setting the variables

First we define the variables of the code. Use local variables to speed up indexing.

local timing = {
    opening = 1, -- Time the door takes to open.
    open = 3,    -- Time the door stays open
    closing = 1  -- Time the door takes to close
}

local steps = 10 -- How smooth the animation of the part sliding is. The higher, the smoother.

local closed = true -- 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

Opening the door

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 closed then
        closed = false
        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 * timing.opening)
            part.CFrame = initialCF + movement*i
        end
        
        wait(timing.open)
        
        for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps.
            wait(step * timing.closing)
            part.CFrame = initialCF + movement*i
        end
        closed = true
    end
end

Binding it to a MouseClick event

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

Complete script

And now, this is what we get if we combine the script:

local timing = {
    opening = 1, -- Time the door takes to open.
    open = 3,    -- Time the door stays open
    closing = 1  -- Time the door takes to close
}
          
local steps = 10 -- How smooth the animation of the part sliding is. The higher, the smoother.
local closed = true -- 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 closed then
        closed = false
        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 * timing.opening)
            part.CFrame = initialCF + movement*i
        end
        
        wait(timing.open)
        for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps.
            wait(step * timing.closing)
            part.CFrame = initialCF + movement*i
        end
        closed = true
    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