Hi, I want to make my door go to another position when it's touched. But somehow it doesn't work with this script:
door = script.Parent.Position open = script.Parent open.Touched:connect(function(h) wait(0.5) door = Vector3.new(7, 3.5, -13) wait(0.2) door = Vector3.new(6, 3.5, -13) wait(0.2) door = Vector3.new(5, 3.5, -13) wait(0.2) door = Vector3.new(4, 3.5, -13) wait(0.2) door = Vector3.new(3, 3.5, -13) wait(0.2) door = Vector3.new(2, 3.5, -13) wait(0.2) door = Vector3.new(1, 3.5, -13) wait(0.2) door = Vector3.new(0, 3.5, -13) wait(1) door = Vector3.new(8, 3.5, -13) end)
Please help, and I'm very new to scripting.
The problem with your script is the door
variable is set to the position when it needs to be set to the object. Because of this, the variable door
is useless, therefore in the script below it will be removed.
Here's your revised script:
local open = script.Parent open.Touched:connect(function(h) wait(0.5) open.Position = Vector3.new(7, 3.5, -13) wait(0.2) open.Position = Vector3.new(6, 3.5, -13) wait(0.2) open.Position = Vector3.new(5, 3.5, -13) wait(0.2) open.Position = Vector3.new(4, 3.5, -13) wait(0.2) open.Position = Vector3.new(3, 3.5, -13) wait(0.2) open.Position = Vector3.new(2, 3.5, -13) wait(0.2) open.Position = Vector3.new(1, 3.5, -13) wait(0.2) open.Position = Vector3.new(0, 3.5, -13) wait(1) open.Position = Vector3.new(8, 3.5, -13) end)
I tried to make as minimal changes as possible to make sure the code isn't unfamiliar to you.
If this code doesn't work for you, make sure to comment and we can work it out!
Instead of using the Position
property, you should the CFrame
property. Using open.Position
can lead to unexpected and unwanted results. Using CFrame is easy, just instead of
open.Position = Vector3.new(0,0,0)
You do
open.CFrame = CFrame.new(0,0,0)
You should practice using CFrame
instead of Position
.
Also, to make your script easy to read, use a for
loop, like this:
local open = script.Parent open.Touched:connect(function(h) wait(.5) for i = 8,1,-1 do open.CFrame = CFrame.new(i, 3.5, 13) wait(.2) end wait(.8) open.CFrame = CFrame.new(8, 3.5, 13) end)
Let me break down what that does. A for
loop has three parameters, the start, the end, and the increment. Here, the start is 8, the end is 1, and the increment is -1. That means the i
, the header variable, will start the loop as 8, and every time the loop runs it will decrease by -1 until it hits 1, meaning the loop runs 8 times.
If you are confused, just reference this wiki page: http://wiki.roblox.com/index.php?title=Loops#For