Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Most efficient way to change lights?

So, I've been working on a vehicle's emergency lighting for the last few days and it works well in an empty game, but in live use it tends to get quite slow in the emergency lights. The coding essentially involves going light by light and telling it to be on or off. Is there a more efficient way to do this?

lightbar.part1.Enabled = true
lightbar.part2.Enabled = false

multiply that by about 20 for every single section of the pattern (tends to have 5-6 different sections)

If you don't really understand what I mean, I am trying to make a emergency lightbar with realistic emergency patterns... look up emergency lighting patterns

For example, here is how it is in the live server: https://gyazo.com/74045ad0678d5293d5be7a0d1b7f044f

And this is what it should look like: https://gyazo.com/74045ad0678d5293d5be7a0d1b7f044f

function turnLightOff(IDs, a, original)
    for k,v in pairs(a) do
        for l,x in pairs(IDs) do
            if v.Name == x then
                v.Lighto.Enabled = true
            else
                v.Lighto.Enabled = false
            end
        end
    end
end
turnLightOff({"B2","R1","R3","B13","B15","R12","R14"}, MainVeh:GetChildren())
wait(10)
turnLightOff({"B1","B3","R2","B12","B14","R13","R15"}, MainVeh:GetChildren())

3 comments

2

use a for loop

1

im so confused what hes asking for some reason

0

Can't use a for loop as some go off and some go on... I am trying to find a more efficient way to turn a lot of lights on and off at the same time.

1 answer

✓ Accepted answer

Is this what you wanted?

function turnLightOff(IDs, a, original)
	for k,v in pairs(a) do
		for l,x in pairs(IDs) do
			if v.Name == original..x do
				v.Enabled = true
			else
				v.Enabled = false
			end
		end
	end
end
--[[
	FUNCTION ARGUMENTS:
		[Table] IDs = The part numbers you want to be turned off.
		[Table] p =  The table the parts are in.
		[String] original = The original name of the part without the ID in it. 
--]]
--EXAMPLE:
--lets say workspace has 10 lights in it. All named: "Part1", "Part2", "Part3" etc.
turnLightOff({5,6}, game.Workspace:GetChildren(), "Part") --run this
--All of the light in workspace have turned off except for "Part5" and "Part6", those two are still on.

In your case:

function turnLightOff(IDs, a, original)
    for k,v in pairs(a) do
        for l,x in pairs(IDs) do
            ifif v.Name == original..x then
                v.Lighto.Enabled = true
            else
                v.Lighto.Enabled = false
            end
        end
    end
end
turnLightOff({"2","13","15"}, MainVeh:GetChildren(),"B")
turnLightOff({"1","3","14"}, MainVeh:GetChildren(),"R")
wait(10)
turnLightOff({"1","3","12","14"}, MainVeh:GetChildren(),"B")
turnLightOff({"2","13","15"}, MainVeh:GetChildren(),"R")

20 comments

0

Thats actually a very smart way to have a single light left on, but I'm working on a whole lightbar which will have say, lights 3, 5, 7, 8, 10, 12, 13, 17 on and all the others off for .25 of a second then swap them back, then do it in a different order like 1,2,4,6,9,12

0

Oh okay, so you want multiple light to be on and some off at the same time, let me edit it for you.

0

I just updated my main post with an example gif of the lights as they are

0

Doesn't this do that? turnLightOff({1,2,4,6,9,12}, game.Workspace:GetChildren(), "Part"), turnLightOff({3, 5, 7, 8, 10, 12, 13, 17}, game.Workspace:GetChildren(), "Part")?

0

Sorry, I hadn't noticed your update until after I said that, I'm currently trying to write it in and test it

0

I feel like I must have made a mistake somewhere it stops after the first light...

0

Can you send me the error?

0

Its not sending the error, I'll add my edited code into the orig post

0

Ah, see you wrote: "B2" which shoud be just "2" or 2. The "B" shoud come after MainVeh:GetChildren(). This is how it should look: turnLightOff({"2","1","3","13","15","12","14"}, MainVeh:GetChildren(), "B")

0

The reason I put the B in there is due to the issue of I have them color coded by B and R for blue and red... I removed the addition of the number with the letter in the function, does that work?

0

you also have to add the "B" at the end of the function.

0

I'll edit the awnser again now I know your parameters

0

Edited!

0

Alright, I simply cant figure out why it only turns one on and leaves all the others off

0

All of the lights need to have the same parent, maybe that's your problem?

0

Hmmm, this is odd, even the one you gave me does the same thing where it stops after one light...

0

so, How its currently trying to go it appears its looking at all the children of the lightbar and compares them against the list, that part should work, then if its correct it takes the light and activates it, but it doesnt activate most of them

0

!! I found it, it was me being an idiot. Replace: if v.Name == x then with if v.Name == original..x do

0

I've also edited my awsner, gtg now tho. If you have any other questions, I'll awsner them tomorrow

0

Alright, thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.