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

From Legacy Roblox Wiki
Jump to navigationJump to search
>MrNicNac
New page: 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. <pre...
 
>MrNicNac
New page: 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. <pre...
(No difference)

Revision as of 16:53, 10 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 opening the door.
local Time = 3   -- How long the door takes to close once opened.
local Smoothness = 10 --How smooth the animation of the door opening 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.


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 Running == false then
      Running = true
      for i = 1, Smoothness do --We will run a loop iterating from 1 to the Smoothness value.
         wait(Speed/Smoothness) --Wait for (Speed/Smoothness). This ensures that it'll only wait for 'Speed' seconds.
         Part.CFrame = Part.CFrame + Vector3.new(1,0,0)
      end
      for i = 1, Smoothness do --We will run a loop iterating from 1 to the Smoothness value.
         wait(Time/Smoothness) --Wait for (Time/Smoothness). This ensures that it'll only wait for 'Time' seconds.
         Part.CFrame = Part.CFrame - Vector3.new(1,0,0)
      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 x,z in pairs(script.Parent:GetChildren()) do
   if z.Name == "Button" then -- Change Button to the name of what needs to be clicked to open the door.
      z.ClickDetector.MouseClick:connect(Open)
   end
end

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

local Speed = 1  -- Speed of opening the door.
local Time = 3   -- How long the door takes to close once opened.
local Smoothness = 10 --How smooth the animation of the door opening 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.

function Open()
   if Running == false then
      Running = true
      for i = 1, Smoothness do --We will run a loop iterating from 1 to the Smoothness value.
         wait(Speed/Smoothness) --Wait for (Speed/Smoothness). This ensures that it'll only wait for 'Speed' seconds.
         Part.CFrame = Part.CFrame + Vector3.new(1,0,0)
      end
      for i = 1, Smoothness do --We will run a loop iterating from 1 to the Smoothness value.
         wait(Time/Smoothness) --Wait for (Time/Smoothness). This ensures that it'll only wait for 'Time' seconds.
         Part.CFrame = Part.CFrame - Vector3.new(1,0,0)
      end
      Running = false
   end
end

for x,z in pairs(script.Parent:GetChildren()) do
   if z.Name == "Button" then -- Change Button to the name of what needs to be clicked to open the Part.
      z.ClickDetector.MouseClick:connect(Open)
   end
end