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 7 years ago
Edited 7 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:

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.

2 answers

Log in to vote
1
Answered by
TrippyV 314 Donator Moderation Voter
7 years ago
Edited 7 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:

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!

0
Thanks! ItzMeDenise 18 — 7y
Ad
Log in to vote
0
Answered by 7 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

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

Answer this question