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
01 | game.ReplicatedStorage [ "Light Settings" ] [ "Left Lights" ] .OnServerEvent:Connect( function () |
02 |
03 | local Switch = script.Parent.Switch |
04 | local ConLights = script.Parent [ "Light Arrow" ] :WaitForChild(script.Parent [ "Light Arrow" ] .LPart, 12 ) |
05 | while Switch.Value = = true do |
06 | task.wait() |
07 | for _, ConLights in ipairs (script.Parent [ "Light Arrow" ] :GetChildren()) do |
08 | ConLights.Transparency = 0 |
09 | wait( 1 ) |
10 | ConLights.Transparency = 1 |
11 |
12 | end |
13 | end |
14 | 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.
01 | game.ReplicatedStorage [ "Light Settings" ] [ "Left Lights" ] .OnServerEvent:Connect( function () |
02 | local Switch = script.Parent.Switch |
03 | local LightArrow = script.Parent [ "Light Arrow" ] |
04 |
05 | while Switch.Value = = true do |
06 | task.wait() |
07 |
08 | for _, ConLights in ipairs (LightArrow:GetChildren()) do |
09 | task.spawn( function () |
10 | ConLights.Transparency = 0 |
11 | task.wait( 1 ) |
12 | ConLights.Transparency = 1 |
13 | end ) |
14 | end |
15 | end |
16 | 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:
01 | game.ReplicatedStorage [ "Light Settings" ] [ "Left Lights" ] .OnServerEvent:Connect( function () |
02 | local Switch = script.Parent.Switch |
03 | local LightArrow = script.Parent [ "Light Arrow" ] |
04 |
05 | while Switch.Value = = true do |
06 | task.wait() |
07 |
08 | for _, ConLights in ipairs (LightArrow:GetChildren()) do |
09 | ConLights.Transparency = 0 |
10 | wait( 1 ) |
11 | ConLights.Transparency = 1 |
12 | end |
13 | end |
14 | end ) |