Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Issue with a rotating lightbar. Please help?

Asked by
unmiss 337 Moderation Voter
9 years ago
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.

0
Edit: Also, the rotate part doesn't work. If I use vector3, none of them spin but one, and it ends up on top. I guess i have to use CFrame but I honestly don't get it ;.; unmiss 337 — 9y

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

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.

0
That's not my issue. My issue is rotation, but I suppose this can solve one error. unmiss 337 — 9y
0
It is your issue though. You posted broken code and this is what is wrong with it. 1waffle1 2908 — 9y
0
Once again, I appreciate it, but unfortunately this still does not solve it. I continue to have rotation problems unmiss 337 — 9y
0
Your other problem was with nested loops. 1waffle1 2908 — 9y
View all comments (2 more)
0
Your answer does not work-the pairs thing. I'm doing it manually. Apparently Anges & FromEurlerAngles XYZ are not valid members? unmiss 337 — 9y
0
Both of those are spelled incorrectly, but in this script you're using Rotation. 1waffle1 2908 — 9y
Ad

Answer this question