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)
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)
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)