Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a script where it's touched, it will move?

Asked by 8 years ago
Edited 8 years ago

Hi, I want to make my door go to another position when it's touched. But somehow it doesn't work with this script:

01door = script.Parent.Position
02open = script.Parent
03 
04open.Touched:connect(function(h)
05    wait(0.5)
06    door = Vector3.new(7, 3.5, -13)
07    wait(0.2)
08    door = Vector3.new(6, 3.5, -13)
09    wait(0.2)
10    door = Vector3.new(5, 3.5, -13)
11    wait(0.2)
12    door = Vector3.new(4, 3.5, -13)
13    wait(0.2)
14    door = Vector3.new(3, 3.5, -13)
15    wait(0.2)
View all 24 lines...

Please help, and I'm very new to scripting.

2 answers

Log in to vote
1
Answered by
TrippyV 314 Donator Moderation Voter
8 years ago
Edited 8 years ago

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:

01local open = script.Parent
02 
03open.Touched:connect(function(h)
04    wait(0.5)
05    open.Position = Vector3.new(7, 3.5, -13)
06    wait(0.2)
07    open.Position = Vector3.new(6, 3.5, -13)
08    wait(0.2)
09    open.Position = Vector3.new(5, 3.5, -13)
10    wait(0.2)
11    open.Position = Vector3.new(4, 3.5, -13)
12    wait(0.2)
13    open.Position = Vector3.new(3, 3.5, -13)
14    wait(0.2)
15    open.Position = Vector3.new(2, 3.5, -13)
View all 22 lines...

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!

0
Thanks! ItzMeDenise 18 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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

1open.Position = Vector3.new(0,0,0)

You do

1open.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:

01local open = script.Parent
02 
03open.Touched:connect(function(h)
04    wait(.5)
05    for i = 8,1,-1 do
06        open.CFrame = CFrame.new(i, 3.5, 13)
07        wait(.2)
08    end
09    wait(.8)
10    open.CFrame = CFrame.new(8, 3.5, 13)
11end)

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

Answer this question