How do I make this script run faster?

Ah, I get it.
The reason I asked if the script is in each vehicle or in the workspace or a script folder is:

  • if the script is in the workspace or a folder every single light will flash at random times, but at the same time as all the other lights in the game.
  • if there is a script is in each vehicle then it should only control the lights in that vehicle, not every light in the game.

The script that controls the lights? If so @Lobestone told me to put the script in Game > StarterPlayer > StarterPlayerScripts, his script makes the client render the flashing, which is probably why the lights all flash at once, but I can’t explain what I mean.

Here’s his script;

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.08)
            setVisibility(v,"FlashingLight2",1)
            wait(0.08)
            setVisibility(v,"FlashingLight2",0)
            wait(0.08)
            setVisibility(v,"FlashingLight2",1)
            wait(0.08)            
            setVisibility(v,"FlashingLight2",0)
            wait(0.08)
            setVisibility(v,"FlashingLight2",1)
            wait(0.08)
            setVisibility(v,"FlashingLight2",0)
            wait(0.08)
            setVisibility(v,"FlashingLight2",1)
            wait(0.8)
        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)

All other scripts I tried/heard were super complicated and also didn’t seem to work at first. One guy mentioned something that probably would’ve worked very well, but I still am learning to script and I’m not supposed to be asking people for scripts. Pretty sure they say you shouldn’t be asking people to write entire scripts for ya.

Ok, but if you’ve got this script in StarterPlayerScripts it’s going to affect every light whether it’s named ‘FlashingLight’ or ‘FlashingLight2’ at the same time.
So if I understand it correctly when you have 1000 ‘FlashingLight’ parts in your game while playing it the Client sided script will flash them all on and off at the exact same time.
My suggestion was to have a script like this to collect all the ‘FlashingLight’ and ‘FlashingLight2’ parts with a script in each vehicle so the wait(Rand:NextNumber(0.01, 0.1)) line would affect each vehicle differently and the lights of all the vehicles wouldn’t flash at the same time.

1 Like

I would be concerned that 1000 scripts running all at once could create a bottleneck during gameplay, especially with wait times of only 0.03 seconds. I would suggest as a longer term fix:

  1. dumbing down the sequence based on distance. The quick flashes aren’t very visible from far off anyway. Use those for foreground vehicles and move the background vehicles to a slower pattern.
  2. if the pattern is slow enough at a distance, you can stagger them in a queue in a single script. Lets say the pattern is on/off every half second. There are 60 frames a second, so 30 frames per half second. If you stagger the flashing, at most, you are only processing 1/30th of the cars at once. 1000 cars? No, 33 cars. That should speed you up a lot.

You would need:

  1. A function to sort all cars into a list, shuffle them for good measure, then split them into different heartbeats.
  2. A function to disable the master list for any car within a certain distance to run the fast blinking sequence.
  3. Reverse of #2, to re-enable the cars as you get back out of range.
  4. Distance: A function to call blinking on each car, based on the frame/heartbeat.
  5. Close: A function in every car to handle fast blinking. (what you currently have, modified to turn off when the distance blinking turns on)
1 Like