GetDescendants issue

Hi there!

I’m currently running into an issue in my code below:

for i, v in pairs(script.Parent:GetDescendants()) do
	if v.Name == "Lightning_Light" then
		lightning_light = v.LightningLight
		lightning_sfx = v.Lightning_SFX
	end
end

while true do
	
	wait(math.random(0, 5))
	
	lightning_light.Brightness = 4
	wait(math.random(0, 0.1))
	lightning_light.Brightness = 0
	lightning_sfx:Play()

end

What this code does is get all instances in all parts named Lightning_Light and gets 2 objects inside of them:

a) Lightning_Light which is a surface light
b) Lightning_SFX which is sound

The issue however is that it only sets the brightness and plays the sound for one part. Any feedback or help is appreciated! :slight_smile:

for i, v in pairs(script.Parent:GetDescendants()) do
	if v.Name == "Lightning_Light" then
		spawn(function()
			while true do 
				task.wait(math.random(0, 5))
				v.LightningLight.Brightness = 4 
				task.wait(math.random(0, 0.1))
				v.LightningLight.Brightness = 0 
				v.Lightning_SFX:Play()
			end
		end)
	end
end

1 Like

Ok, while it does work now, the brightness and sound effect don’t synchronously play across all the parts. Would there be any way to do this?

they dont play synchronously because you put a math.random inside the wait so it waits a different amount of time in all of the Lights, if you want all the lights to wait the same time then use this code:

local RandomTime1 = math.random(0, 5)
local RandomTime2 = math.random(0, 0.1)

for i, v in pairs(script.Parent:GetDescendants()) do
	if v.Name == "Lightning_Light" then
		spawn(function()
			while true do 
				task.wait(RandomTime1)
				v.LightningLight.Brightness = 4 
				task.wait(RandomTime2)
				v.LightningLight.Brightness = 0 
				v.Lightning_SFX:Play()
			end
		end)
	end
end

1 Like

Yup you’re right lol

Thanks for the help! =)