How do I make this script run faster?

Warning: I suck at scripting, so I apologize if I don’t understand your answers/questions!

I’ve made a little car with some emergency lights on it, and when I put the vehicle into a large game I’d like the lights to flash at normal speed.

The issue is, the lights are flashing very slowly… Although this is understandable due to how large the game is.

I’ve tried looking for anything on how to make scripts run faster but since I’m not really a master at code all of the solutions were very confusing to me. I know that internet connection matters in this but I was hoping there was a way to make it so people with a slower connection can still see the lights flash the same. I’ve experienced a couple of times when this game didn’t have the slow lights and they were flashing at normal speed, but that’s only happened once.

Edit: I’ve tried StreamingEnabled, definitely doesn’t work.

Here’s the script for one of the lights shown on the vehicle. (Basically, every other script is the same)

local Rand = Random.new()

while true do
    wait(Rand:NextNumber(0.01, 0.1))
    if script.Parent.Parent.on.Value == true then
        script.Parent.Transparency = 0
        wait(0.03)
        script.Parent.Transparency = 1
        wait(0.03)
        script.Parent.Transparency = 0
        wait(0.03)
        script.Parent.Transparency = 1
        wait(0.03)
        script.Parent.Transparency = 0
        wait(0.3)
        script.Parent.Transparency = 1
        wait(0.3)
    else
        script.Parent.Transparency = 1
    end
end

If anyone needed videos here they are:

Small Game Lights Flashing:

Large Game LIghts Flashing:

Any ideas on how to maybe make it run faster?

1 Like

Seems to me like both the lights are 2 different scripts make 1 script handle all the lights then if one light is slower both lights will be so it will go on and off together. also the random numer generator at the top of your script does not help holding them in sync.

1 Like

It’s best for fast flashing items to be inside a localscript and have all clients render the flashing effect.

1 Like

And how many lights in total are scripted like this?
If you group all the items in one vehicle to flash with a local script as @Lobestone said it should help a bit.

The issue I see is that it slows down according to the size of the game you are in. That indicates it’s the game that is the cause of the lag, not the light script.

It’s not necessarily the game causing issues, it’s the unreliable nature of how fast parts will update when more players join the game.

Changing a part on the server really fast will NOT guarentee that the client will see the same thing.

Since I’m not the best at scripting I may not be understanding your message very well, so sorry about that, but how would I go about making the clients render the flashing effect? And when you say “It’s best for fast flashing items to inside a localscript…” you mean by having the same script I have shown, just in a localscript?

Edit: The guy above you also mentioned the lights being in sync, I couldn’t really understand if he was saying they were supposed to be in sync or if they weren’t. I don’t really want them to be in sync, just for realism.

Quite a lot of lights are scripted like this. In fact, basically every light in the game is scripted like this.

Use TweenService instead.

local TweenService = game:GetService("TweenService")

local light = script.Parent
local flashTween = TweenService:Create(light, TweenInfo.new(0.05, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut, 3, true), {Transparency = 0})

local on = light.Parent.on.Value

light.Parent.on.Changed:Connect(function(value)
    on = value
end

while task.wait(math.random() * 0.5) do
    if on then
        flashTween:Play()
        flashTween.Completed:Wait()
    end
end

This is probably because I haven’t done the rendering part but I’m not 100% sure, in the large game the LocalScripts made the lights not flash at all. They were just visible doing nothing. But once again probably because I haven’t done the rendering part.

Had the same issue with this, lights not flashing. I noticed it was asking me to put a “)” on “end” after the “light.Parent.on.Changed:…” so I did that. Both with and without ending that, the lights did nothing. Am I doing something wrong?

Edit: once again, I suck at scripting, so I’m very sorry if I’m not understanding your guys’ messages correctly.

Waits aren’t really useful here. You can use TweenService as said or you can try task.wait() instead of wait(). Maybe that can be helpful.

Make sure that you got the most recent edit. Also make sure that the on variable is true, so turn on the value in the “on” instance.

Using task.wait() instead of my script might solve it since it’s connected to the render.

local TweenService = game:GetService("TweenService")

local light = script.Parent
local flashTween = TweenService:Create(light, TweenInfo.new(0.05, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut, 3, true), {Transparency = 0})

local on = light.Parent.on.Value

light.Parent.on.Changed:Connect(function(value)
    on = value
end)

while task.wait(math.random() * 0.5) do
    if on then
        flashTween:Play()
        flashTween.Completed:Wait()
    end
end

Although this may not be the best example to use in a localscript, this would work.

Have the part name be FlashingLight
and simply drop this localscript into StarterPlayerScript and disable the server script.

This will entirely offload the part’s flashing function to the client to handle, therefore reducing any network overhead by a simple on variable. So as long as the client doesn’t lag at all, this would work.

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
	pcall(function()
		if v.Name=="FlashingLight" then
			HookLight(v)
		end
	end)
end
workspace.DescendantAdded:Connect(function(v)
	pcall(function()
		if v.Name=="FlashingLight" then
			HookLight(v)
		end
	end)
end)

If you do want to slightly optimize this to not be so demanding on checking for a new vehicle to be added, the DescendantsAdded Connected event can be changed to ChildAdded and check if the name of the new model in workspace matches the vehicle model name.

This is definitely gonna sound like a super noob question to you, and I apologize, but what exactly is the “server script” you’re wanting me to disable? The flashing script in the lights or something else? I assume you mean the flashing script but I just wanted to make sure.

Server scripts are Scripts. This is just an alternate name for them since the server runs these scripts, not the player. Local scripts are run by the player instead.

He’s telling you to disable or remove all of original light script you’re using and to instead add a single Local script into StarterPlayerScripts or something.

Using local scripts helps reduce the lag from the server by letting the player run the heavy code and rendering instead of the server.

1 Like

Yeah the lights are just sitting there visible, not flashing. Should “on” not be true when I load up the game, or maybe the lights should be Transparent?

Maybe replace this part of the code with this:

local flashTween = TweenService:Create(light, TweenInfo.new(0.05, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut, 3, true), {Transparency = 1})

Try this version instead and see if anything prints in the output.

Be sure the part you want flashing is named FlashingLight and in the ParentParent of FlashingLight, the on value exists.

This should be inserted into a localscript and put into StarterPlayer > StarterPlayerScripts

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(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(y)
	end 
end)

You saying output just made me remember to tell you this and chances are, this is probably part of the problem;

Due to how large the game is, the output gets filled with so many errors INSTANTLY. So that would take me a while to fix up. Although none of the errors are errors that should be that bad, they’re just errors telling me some lights don’t exist, and I’m aware of that. Just got to delete a lot of scripts and that should remove most errors.

If the lights are always flashing (player can’t turn them on/off) and you want the lights to flash alternating (LH on, RH off, then RH on LH off) you could also rewrite @Lobestone’s script to have 2 groups of lights and have group 1 turn on while group 2 turns off, then switch them so 1 is off while 2 is on.