How do I run a script for all parts in a model with wait() blocks?

I have tried this script:

for i, part in pairs(Lib.Map:GetDescendants()) do
		if part.Name == "FacilityLight" and part:IsA("BasePart") then
			local Chance = math.random(1, 3)
			if Chance == 1 then
				local SaveColor = part.Color
				part.Color = Color3.fromRGB(0, 0, 0)
				if part:FindFirstChild("SpotLight") then
					part:FindFirstChild("SpotLight").Enabled = false
				end
				wait(0.1)
				part.Color = SaveColor
				if part:FindFirstChild("SpotLight") then
					part:FindFirstChild("SpotLight").Enabled = true
				end
			end
		end
	end

This script should run through all the parts in the model and if the part passes a random chance test, it should turn black, a light inside it should turn off, then wait 0.1 seconds, then revert this. I want this to happen in synchrony for every part in the model, but the script would instead wait 0.1 seconds and pause for each part. How do I fix this?

1 Like

Use task.spawn so the wait inside the light flicker doesn’t interfere with the rest of the script.

for i, part in pairs(Lib.Map:GetDescendants()) do
	if part.Name == "FacilityLight" and part:IsA("BasePart") then
		local Chance = math.random(1, 3)
		if Chance == 1 then
			task.spawn(function()
				local SaveColor = part.Color
				part.Color = Color3.fromRGB(0, 0, 0)
				if part:FindFirstChild("SpotLight") then
					part:FindFirstChild("SpotLight").Enabled = false
				end
				wait(0.1)
				part.Color = SaveColor
				if part:FindFirstChild("SpotLight") then
					part:FindFirstChild("SpotLight").Enabled = true
				end
			end)
		end
	end
end
2 Likes

It doesn’t seem to work, it still waits the 0.1 seconds

1 Like

Still waiting for an answer

1 Like

answer she gave you should work, not working because youre prob using :getdescendants() to get the part and that will take alot of time if theres alot of parts so instead of that put every facilitylight in a folder

local facilityLights = Lib.Map.FacilityLights
local flicker = {}
for _, part in pairs(facilityLights:GetChildren()) do
	local Chance = math.random(1, 3)
	if Chance == 1 then
		table.insert(flicker, part)
	end
end
for _, part in pairs(flicker) do
	task.spawn(function()
		local SaveColor = part.Color
		local SpotLight = part:FindFirstChild("SpotLight")
		part.Color = Color3.fromRGB(0, 0, 0)
		if SpotLight then
			SpotLight.Enabled = false
		end
		task.wait(0.1)
		part.Color = SaveColor
		if SpotLight then
			SpotLight.Enabled = true
		end
	end)
end

Don’t put it in a folder otherwise the light won’t work. Use collection service instead

its going to work inside a folder what are u on, a folder is like a model

Do I have to use a table or can I just move them into a folder?

juse move them inside a folder inside Lib.Map called FacilityLights

Interesting cause when I put it in a folder during a different project it didnt work at all

You can reduce logic checks here if you use collection service. Just tag all the “FlickerPart” parts upon creation/iteration and then iterate them at your leisure using CollectionService:GetTagged("FlickerParts").

Then there is no need to check whether they are this or that, GetTagged() will only return parts that you have ear-marked for flickering.

With regard to syncing the on/off effect you will have to use a single chance variable (before the loop) to make sure they do the same thing at the same time (note. this may not be reliably true due to replication, CPU, broadband, and many other things that affect timing, but the human eye might not detect any defect here, so this simple system might work if the number of “FlickerPart” is not an overwhelming amount!)…

local CollectionService = game:GetService("CollectionService");
local FLICKERPART_TAG = "FlickerPart";

-- tag them all!
for i, part in pairs(Lib.Map:GetDescendants()) do
	if part.Name == "FacilityLight" and part:IsA("BasePart") and part:FindFirstChild("SpotLight") then
		CollectionService:AddTag(part,FLICKERPART_TAG );
	end
end

-- do something with them all!
local FlickerParts = CollectionService:GetTagged(FLICKERPART_TAG);
local Chance = math.random(1, 3);
for i, part in pairs(FlickerParts) do
	-- no need to check if part is "FlickerPart" since collection service will do this for you
	if Chance == 1 then
		local SaveColor = part.Color;
		part.Color = Color3.fromRGB(0, 0, 0);
		-- no need to check if part has a "SpotLight" because you tag only parts with a SpotLight as a child
		part.SpotLight.Enabled = false;
		wait(0.1);
		part.Color = SaveColor;
		part.SpotLight.Enabled = true;
	end
end
1 Like

Found the problem! If you use Delay instead of Wait youll get the same thing to work but at the same time :))

for i, part in pairs(Lib.Map:GetDescendants()) do
		if part.Name == "FacilityLight" and part:IsA("BasePart") then
			local Chance = math.random(1, 3)
			if Chance == 1 then
				local SaveColor = part.Color
				part.Color = Color3.fromRGB(0, 0, 0)
				if part:FindFirstChild("SpotLight") then
					part:FindFirstChild("SpotLight").Enabled = false
				end
				delay(0.1,function()
                                part.Color = SaveColor
				     if part:FindFirstChild("SpotLight") then
				    	    part:FindFirstChild("SpotLight").Enabled = true
				      end)
			end
		end
	end
1 Like