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

Am I using GetChildren() correctly?

Asked by 10 years ago
script.Parent.OnTouch:connect(function()
    for i,p in pairs(script.Parent.Parent.Truck:GetChildren()) do
        for i = 1,200 do
            p.CFrame = p.CFrame * CFrame.new(.4,0,0)
            wait(.1)
        end
    end
end)

For some reason, this script doesn't work. It works with changing size, but not position. There's also this script:

script.Parent.OnTouch:connect(function()
    wait(6)
        for i,p in pairs(script.Parent.Parent.Parts:GetChildren()) do
            for i = 1,10 do
                p.CFrame = p.CFrame * CFrame.new(.25,0,0)
                wait(.1)
            end
        p.Anchored = false
    end
end)
0
Keep in mind when you use "GetChildren()". They take ALL the children of the parent. Including scripts and such that do not have positions or CFrame Necrorave 560 — 10y
0
The only things in the models "Truck" and "Parts" are bricks. Lightdrago 95 — 10y

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago
script.Parent.OnTouch:connect(function()

There is no OnTouch event. That is what people sometimes name their functions that they plan to connect to the Touched event, but the actual event is called Touched, not OnTouch. So,

script.Parent.Touched:connect(function()

should work.

for i,p in pairs(script.Parent.Parent.Parts:GetChildren()) do
            for i = 1,10 do

You have two i variables. I would recommend that you rename one of them to something else.

for index,p in pairs(script.Parent.Parent.Parts:GetChildren()) do
            for i = 1,10 do

I would also recommend that you add a debounce. A debounce is just a bool value that prevents the touched event from being triggered many times at once.

local debounce = false

script.Parent.OnTouch:connect(function()
    if debounce == false then
        debounce = true --Prevents the code from running until it equals false again.
        for i,p in pairs(script.Parent.Parent.Truck:GetChildren()) do
            for i = 1,200 do
                p.CFrame = p.CFrame * CFrame.new(.4,0,0)
                wait(.1)
            end
        end
        debounce = false --Lets it run again
    end
end)
Ad

Answer this question