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 19: Line 19:
== Opening the door ==
== 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 openAndCloseDoor() 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 a [[debounce]] variable.


  function Open()
  function openAndCloseDoor()
     if closed then
     if closed then
         closed = false
         closed = false
Line 48: Line 48:
  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(openAndCloseDoor)
     end
     end
  end
  end
Line 61: Line 61:
     closing = 1  -- Time the door takes to close
     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 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 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 part = script.Parent          -- The part to slide.
  local movement = Vector3.new(5, 0, 0) -- Total movement between when door is open and closed
  local movement = Vector3.new(5, 0, 0) -- Total movement between when door is open and closed
   
   
  function Open()
  function openAndCloseDoor()
     if closed then
     if closed then
         closed = false
         closed = false
Line 80: Line 80:
          
          
         wait(timing.open)
         wait(timing.open)
 
       
         for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps.
         for i = 1, 0, -step do --We will run a loop iterating from 1 to 0 in `Steps` steps.
             wait(step * timing.closing)
             wait(step * timing.closing)
Line 90: Line 90:
  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(openAndCloseDoor)
     end
     end
  end
  end

Revision as of 16:22, 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 openAndCloseDoor() 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 a debounce variable.

function openAndCloseDoor()
    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(openAndCloseDoor)
    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 openAndCloseDoor()
    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(openAndCloseDoor)
    end
end