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

Why is my function running for my models children one at a time instead of all at once?

Asked by
Faazo 84
6 years ago
Edited 6 years ago

I have this door that I scripted to open when you click the handle, but for some reason instead of "opening" all the parts at once, it opens them one at a time. Here is my script:

local handle1 = script.Parent.Open.Part1

handle1.ClickDetector.MouseClick:Connect(function()
    local function Open(part)
        part.Transparency = 1
        part.CanCollide = false
        wait(1)
        part.Transparency = 0
        part.CanCollide = true
        end

        for i, v in pairs(script.Parent.Open:GetChildren()) do
            Open(v)
        end
end)

I used a for i, v in pairs thing to get the children of the door's parent. It opens like this: the board opens, a second later part of the handle opens, then another part of the handle, etc. I'm wondering why it doesn't just open them all at the same time? I also noticed that when there is no wait function it does what its told. Help is appreciated. Thanks.

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

There is a wait(), it's inside the Open() function.

For i,v in pairs runs all the code for the first item inside the table and when it's finished searches for the next one (if there is a next one)

If you call a function it will wait for the function to finish until it continues the code, this is because roblox won't let you run 2 things at once.

So you'll probably understand that there is a wait() inside your code on line 07 and because you call that function it will wait too.

The best way to fix it is to not put the i,v on the loop but on these parts

part.Transparency = 1
part.CanCollide = false

Because they don't have a wait(), then just wait(1) and after that do the same using for i,v in pairs.

If you have any more questions, just ask :)

Ad

Answer this question