local car = game.Workspace:FindFirstChild(script.Parent.Parent.Parent.CarName.Value,true) local lightbar = car:WaitForChild("Lightbar") local rotatingon = false local R1 = lightbar.R1 local R2 = lightbar.R2 local B1 = lightbar.B1 local B2 = lightbar.B2 local R1L = lightbar.R1:GetChildren() local R2L = lightbar.R2:GetChildren() local B1L = lightbar.B1:GetChildren() local B2L = lightbar.B2:GetChildren() local rotators = {R1,R2,B1,B2} script.Parent.MouseButton1Down:connect(function() if not rotatingon then rotatingon = true for i,l in pairs(R1L,R2L,B1L,B2L) do l.Enabled = true end for i,v in pairs(rotators) do while true do v.Rotation = v.Rotation + 5 wait(0.1) if not rotatingon then break end end end end end)
I'm also trying to make other parts flash as well and don't know how to do this at the same time with the rotating. I tried coroutines but it isn't working. This is simply a toggle. It's getting EXTREMELY confusing :( If you need more in-depth info, I can do a join.me Basically, when you click the button, all other lights should be constantly flashing (I have no idea where to put the code for that), the rotator lights should be static and on, and rotating, clearly. Please help.
pairs
does not take multiple arguments, and subtables' indices will not be accessed by calling pairs on their parent table. What you need to do is put all of the children into one table, instead of putting all of the tables into one table.
local rotators={} for _,v in pairs(R1L)do rotators[#rotators+1]=v end for _,v in pairs(R2L)do rotators[#rotators+1]=v end for _,v in pairs(B1L)do rotators[#rotators+1]=v end for _,v in pairs(B2L)do rotators[#rotators+1]=v end
Alternatively, here's a nifty function for grouping multiple tables' numerical indices into one table:
function group(...) local t={} for _,x in pairs{...}do for _,y in ipairs(x)do t[#t+1]=y end end return t end local rotators=group(R1L,R2L,B1L,B2L)
Your other problem is that you have a while loop inside of a for loop, which will prevent the for loop to iterate to the next index until the while loop halts. To solve that, put the for loop inside of the while loop instead.