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

Trouble with "for loops" used to change position of a Union part?

Asked by 9 years ago

My script is below.

base        =   script.Parent.Parent.Parent
dtop        =   base.SecurityDoor.TopDoor
dbottom     =   base.SecurityDoor.BottomDoor

function onClicked()

    for i = 1, 10 do
    dtop.Position.Vector3 = CFrame.new() + Vector3.new(0,1,0)   
    dbottom.Position.Vector3 = CFrame.new() + Vector3.new(0,1,0)
    break

    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

To be specific, I want the parts to go in a single direction by one stud for every loop. Both the TopDoor and BottomDoor are Unions.

This is for my security door. When you click the brick within the Biometric Scanner, it scans your Access Level and, if your Access Level meets or exceeds the minimum, then the script opens the door.

1 answer

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago

Ok, so you had a few errors here. I'll take you through them step by step, and give you the finished script at the end.

for i = 1, 10 do
    dtop.Position.Vector3 = CFrame.new() + Vector3.new(0,1,0)   
    dbottom.Position.Vector3 = CFrame.new() + Vector3.new(0,1,0)
    break
 end

(This chunk is basically where all your errors took place)


First of all, you did:

dtop.Position.Vector3
dbottom.Position.Vector3

This is incorrect because there is no Property for Vector3. Vector3 is a DataType that is used to set physical traits of an object such as Position, Size, Velocity ect. To use Vector3 correctly, you need to do something like this:

dtop.Position = Vector3.new(1,5,1)

Secondly, you tried to add the CFrame Position, and Vector3 Position of the objects together.

CFrame.new() + Vector3.new(0,1,0)   

You also cannot do this. This results in errors, because DataTypes can only be used with their own DataType. Also, you left the arguments in the first part empty. That sets the initial position. To fix this, we'll change the entire script to using CFrame instead of using CFrame and Vector3, and then we'll fill in those values:

dtop.CFrame = CFrame.new(dtop.Position) * CFrame.new(0,1,0) --Also, change to multiplication, not addition
dbottom.CFrame = CFrame.new(dbottom.Position) * CFrame.new(0,1,0) -- ^ Idk why it's like that honestly.

lastly, you had a break in the loop.

break

When you use a break, it ends the current loop that's running. Breaks don't normally need to be used unless you have a function that can run indefinitely which can become an infinite loop. When you used it in your function, it ended the current loop that was running, so that it only moved the blocks up once before it was removed.


Now for the finished script!

base = script.Parent.Parent.Parent
dtop = base.SecurityDoor.TopDoor
dbottom = base.SecurityDoor.BottomDoor

function onClicked()
    for i = 1, 10 do
        wait() --I added this because I assumed you would like to see the doors actually moving, instead of having them teleport up/down.
        dtop.CFrame = CFrame.new(dtop.Position) * CFrame.new(0,1,0)   
        dbottom.CFrame = CFrame.new(dbottom.Position) * CFrame.new(0,1,0)
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

So, I cleaned up your script a bit, and I believe I fixed pretty much everything that was wrong with it. I understand this can be alot to try to learn all at once, so if you have any further problems/questions, please leave a comment below. Hope I helped :P

2
Your finished version of the script still uses the wrong `CFrame.new()`. Also: CFrame + Vector3 is totally fine; and CFrame * CFrame.new(Vector3) is slightly different -- the multiplication will take it in the direction of its Top face, while the + will actually take it UP. BlueTaslem 18071 — 9y
0
Change wait(.1) to wait() since wait() is around 0.03 seconds which is the same speed animations and movies change frames. EzraNehemiah_TF2 3552 — 9y
0
Oh and btw BlueTaslm is right, for Example: CFrame.new(Vector3.new(0,10,0)) would work. EzraNehemiah_TF2 3552 — 9y
0
I knew that Lord, but I didn't know you could do CFrame + Vector3 dyler3 1510 — 9y
Ad

Answer this question