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

Won't get all the children in the model [Light Arrow]??

Asked by 1 year ago

My script is suppose to set the transparency to 0 and 1 for all the parts in [light arrow] but it only does 1 random part in the model

game.ReplicatedStorage["Light Settings"]["Left Lights"].OnServerEvent:Connect(function()

    local Switch = script.Parent.Switch
    local ConLights = script.Parent["Light Arrow"]:WaitForChild(script.Parent["Light Arrow"].LPart, 12)
    while Switch.Value == true do
        task.wait()
        for _, ConLights in ipairs(script.Parent["Light Arrow"]:GetChildren()) do 
            ConLights.Transparency = 0
            wait(1)
            ConLights.Transparency = 1

            end
        end
end)
0
At the same time or one by one? Also you're overwriting ConLights in Line 7, maybe you're trying to edit the ConLights in Line 4 T3_MasterGamer 2189 — 1y
0
At the same time. on line 7 I thought that is the way to get all the parts in the model [Light Arrow] 666_brithday 103 — 1y

2 answers

Log in to vote
1
Answered by 1 year ago

To change all parts at the same time, use a coroutine. Wrap Lines 9-11 in either coroutine.wrap() or task.spawn() (recommended).

For this I'm using @aleandroblingo's answer.

game.ReplicatedStorage["Light Settings"]["Left Lights"].OnServerEvent:Connect(function()
    local Switch = script.Parent.Switch
    local LightArrow = script.Parent["Light Arrow"]

    while Switch.Value == true do
        task.wait()

        for _, ConLights in ipairs(LightArrow:GetChildren()) do
            task.spawn(function()
                ConLights.Transparency = 0
                task.wait(1)
                ConLights.Transparency = 1
            end)
        end
    end
end)
0
Thx for ur help 666_brithday 103 — 1y
0
Wait there is a minor bug I am getting now when I changed the wait time to 4 it doesn't wait 4 seconds it just keeps doing it every second 666_brithday 103 — 1y
0
line 6 or 11? you should change line 6 not 11 T3_MasterGamer 2189 — 1y
0
OK THX 666_brithday 103 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

If I'm correct, this only returns one instance of the specified child, meaning that your ConLights variable only refers to one part in the "Light Arrow" object.

To fix this issue, try to replace the WaitForChild method with the GetChildren method, which will return a table containing all the children of the "Light Arrow" object. Then, you can iterate over this table using a for loop and apply the transparency changes to each part individually.

Try this script:

game.ReplicatedStorage["Light Settings"]["Left Lights"].OnServerEvent:Connect(function()
    local Switch = script.Parent.Switch
    local LightArrow = script.Parent["Light Arrow"]

    while Switch.Value == true do
        task.wait()

        for _, ConLights in ipairs(LightArrow:GetChildren()) do 
            ConLights.Transparency = 0
            wait(1)
            ConLights.Transparency = 1
        end
    end
end)
0
No I am wanting it to change all the parts transparency at the same time. Mine right now does what your script does. 666_brithday 103 — 1y

Answer this question