How do I make this script run faster?

If you have lots of errors, change the warn(y) to warn(“checkme~”,y)

local Rand = Random.new()

function HookLight(v)
	task.spawn(function()
		while true do
			wait(Rand:NextNumber(0.01, 0.1))
			if v.Parent.Parent.on.Value == true then
				v.Transparency = 0
				wait(0.03)
				v.Transparency = 1
				wait(0.03)
				v.Transparency = 0
				wait(0.03)
				v.Transparency = 1
				wait(0.03)
				v.Transparency = 0
				wait(0.3)
				v.Transparency = 1
				wait(0.3)
			else
				v.Transparency = 1
				wait(.5) -- better to wait a bit more if it's off.
			end
		end
	end)
end

for i,v in next,workspace:GetDescendants() do
	local x,y=pcall(function()
		if v.Name=="FlashingLight" then
			HookLight(v)
		end
	end)
	if not x then
		warn("checkme!!",y)
	end 
end
workspace.DescendantAdded:Connect(function(v)
	local x,y=pcall(function()
		if v.Name=="FlashingLight" then
			HookLight(v)
		end
	end)
	if not x then
		warn("checkme!!",y)
	end 
end)

Then in the output in studio, simply filter for ‘checkme’.
image

1 Like

Lights are still not flashing, but no errors coming from the script in StarterPlayerScripts.

Edit: as we are doing this I am also trying my best to lower the amount of errors.

Are the lights you want to flash named FlashingLight?

Was the localscript put into StarterPlayer > StarterPlayerScripts?

1 Like

Both yes. wasn’t sure if the heart was enough to make sure you knew so I have to extend this message

Do you have a screenshot of how your vehicle model looks?
The script works in my example.

as I put the model into a separate game I noticed an error that might explain something. Inside of my car, the FlashingLights are stored in a separate model. Basically the car is: Vehicle > Body > Lightbar > and this is where “on” is stored.

I’m getting a video right now by the way

Oh. This script should do then since the on variable is in the same model as the FlashingLight.

local Rand = Random.new()

function HookLight(v)
	task.spawn(function()
		while true do
			wait(Rand:NextNumber(0.01, 0.1))
			if v.Parent.on.Value == true then
				v.Transparency = 0
				wait(0.03)
				v.Transparency = 1
				wait(0.03)
				v.Transparency = 0
				wait(0.03)
				v.Transparency = 1
				wait(0.03)
				v.Transparency = 0
				wait(0.3)
				v.Transparency = 1
				wait(0.3)
			else
				v.Transparency = 1
				wait(.5) -- better to wait a bit more if it's off.
			end
		end
	end)
end

for i,v in next,workspace:GetDescendants() do
	local x,y=pcall(function()
		if v.Name=="FlashingLight" then
			HookLight(v)
		end
	end)
	if not x then
		warn("checkme!!",y)
	end 
end
workspace.DescendantAdded:Connect(function(v)
	local x,y=pcall(function()
		if v.Name=="FlashingLight" then
			HookLight(v)
		end
	end)
	if not x then
		warn("checkme!!",y)
	end 
end)
1 Like

Aha! Seems to be working. Gotta try this in the large game however.

It’s working! I can’t explain to you how happy I am, I’ve been working on this game for so long now. Thank you so much!! And thanks to everyone else for your suggestions!!

This can get pretty expensive and, isn’t really the best way of creating a pattern which causes lights to flash; this will also cause a memory leak if a light is removed and created again since you’ll be running multiple while true do loops, actually, it will cause the script to stop executing due to running too fast. game to lag due to several loops running.

If you see this, I’d also like to know, since I have multiple vehicles and they all have separate light flashing patterns, how would I go about editing separate light patterns?

A better solution would be to group lights together and have a single loop instead of multiple loops causing the lights to flash, a group would contain a set of lights which will flash and possibly other information about how they should flash and whether they should flash or not, this will be much cheaper since you’ll only have a single loop and, it will be easier to add other patterns in the future.

When a part is removed, the script errors (inside the pcall) and the while true do loop usually dies, therefore it will self clean.

As I’ve said when I first started responding, it’s not the most optimal way, but it’s better than the TweenService or Server sided options still existing and reducing reliance on network replication and network usage.

Client memory leaks necessarily don’t matter severely as server sided leaks as Clients only play for one or two hours, compared to a server staying up for multiple hours.

While this may work, this isn’t really the best way to terminate loops and, this is a pretty expensive way of causing lights to flash with very little modularity, it would probably be much better to do

while v.Parent ~= nil do
    ....
end

I’d still advise against this though, there are betters ways of doing this (e.g. using a module with groups of lights, etc).

Note: I agree, it is probably better than some of the other solutions stated (server sided scripts, tween service, etc) but, this lacks modularity and will run on multiple (virtual) threads.

Your idea sounds like it may work better, but his is just much easier to understand for me. So I think I’ll just stick with his for now, but the only question I have for him is how to add different patterns, which you seem to have a script that makes it easier to do so. Both of your ideas are amazing, I’m not trying to say his sucks or yours sucks, just incase anyone misunderstood.

Like I said, I’m not the greatest scripter, so. Sorry if I sounded a bit rude there, not trying to be.

1 Like

As large as this adjusted script for slightly better customizability and reducing the use of multiple loops for a single model and providing customizability.

This would allow you to make multiple parts with different names and have a different strobe effect.

local Rand = Random.new()

function setVisibility(v,name,transparency)
	for i,v in next,v:GetChildren() do
		if v.Name==name and v:IsA("BasePart") then
			v.Transparency=transparency
		end
	end
end 

local modules={};
--  modules['part name to visualize']
--  if a model with 'on' and the part name in [ -- ] exists, 
modules['FlashingLight']=function(v)
	while v.Parent~=nil do
		wait(Rand:NextNumber(0.01, 0.1))
		if v.on.Value == true then
			setVisibility(v,"FlashingLight",0)  
			--This function will set all items in the model with the name of 'FlashingLight' to the next number.
			wait(0.03)
			setVisibility(v,"FlashingLight",1)
			wait(0.03)
			setVisibility(v,"FlashingLight",0)
			wait(0.03)
			setVisibility(v,"FlashingLight",1)
			wait(0.03)
			setVisibility(v,"FlashingLight",0)
			wait(0.3)
			setVisibility(v,"FlashingLight",1)
			wait(0.3)
		else
			setVisibility(v,"FlashingLight",1)
			wait(.5) -- better to wait a bit more if it's off.
		end
	end
end
modules['FlashingLight2']=function(v)
	while v.Parent~=nil do
		wait(Rand:NextNumber(0.01, 0.1))
		if v.on.Value == true then
			setVisibility(v,"FlashingLight2",0)
			wait(0.3)
			setVisibility(v,"FlashingLight2",1)
			wait(0.3)
		else
			setVisibility(v,"FlashingLight2",1)
			wait(.5) -- better to wait a bit more if it's off.
		end
	end
end



local modulesExisting={}
for i,v in next,modules do
	modulesExisting[i]=true
	-- Helps convert modules into a true/false quick check.
end 

function HookLightGroup(model)
	local foundModules={}
	local foundModNum=0;
	for i,item in next,model:GetChildren() do
		if modulesExisting[item.Name] then
			print("Module for ",item.Name)
			foundModules[item.Name]=true;
			foundModNum+=1;
		end 
	end 
	if foundModNum>0 then
		for modulename,v in next,foundModules do
			print("Running ",modulename)
			task.spawn(function()
				modules[modulename](model)
			end)
		end
	else
		print("No modules found for ",model)
	end
end 

--Handling of grabbing any folder/model with 'on' boolvalue inside.
for i,v in next,workspace:GetDescendants() do
	local x,y=pcall(function()
		if v.Name=="on" and v.ClassName=="BoolValue" then
			HookLightGroup(v.Parent)
		end
	end)
	if not x then
		warn("LocalLight Issue possible, ",y)
	end 
end
workspace.DescendantAdded:Connect(function(v)
	local x,y=pcall(function()
		if v.Name=="on" and v.ClassName=="BoolValue" then
			HookLightGroup(v.Parent)
		end
	end)
	if not x then
		warn("LocalLight Issue possible, ",y)
	end 
end)

You are free to remove the prints if necessary. They’re simply debugging.

1 Like

I’ve noticed an issue with this script and have adjusted it. If a model has multiple parts, it would run the module loop multiple times for the whole module group, please use the edited version instead.

1 Like

Sorry about the late reply.

I’ve noticed that with both FlashingLight’s the random wait doesn’t seem to work. It flashes but the lights don’t start un-syncing, they just all flash at once. Could I be doing something wrong or could something in the script not be right?

Edit: Didn’t read your message correctly. So sorry, I deleted the other part. But still, random wait doesn’t seem to work.

What does the random actually do for you? Are you expecting each vehicle to have the lights flash red/blue/red/blue at separate times?
Do you have the script in game, or in each vehicle?

If you have all the blue lights named ‘FlashingLight’ and the red lights named ‘FlashingLight2’ then @Lobestone’s script should work but I believe that it needs to be in each vehicle separately, not in the workspace.

I’ll try and find a video of the patterns I’m looking for, but what do you mean by “Do you have a script in game, or in each vehicle?”, I just got kinda confused. I can’t reply after this for a while by the way. I can’t really explain what the random wait is really supposed to do, again I could probably find a video.

Edit: I guess just the best way to explain it is how after all the lights turn on, they gradually start un-syncing.

Here’s what FlashingLight2 is supposed to do: (0:40 - 0:47)

And if you can see it, here’s how the lights are supposed to “un-scync”: (Look at the flashing lights above the headlights)

warning that one is loud. I also can’t remember if I’m allowed to post YT links but.