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

for i,v in pairs not working?

Asked by 9 years ago

Everything works fine, just on the Lights1 it has 5 children, Light1, Light2 etc. Only the first 3 lights change to blue (127/225) the others stay white. Lights2 has 2 children and they both go to blue. Here's the script:

Lights1 = game.Workspace.LightSet1.Lights:GetChildren()
Lights2 = game.Workspace.LightSet2.Lights:GetChildren()

function onClick(click)
    for i,v in pairs(Lights1) do
    Lights1[i].Color = Color3.new(0, 0, 127/255)
    Lights2[i].Color = Color3.new(0, 0, 127/255)
    script.Parent.Material = Enum.Material.Neon
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

What's happening is you're getting an error at line 7 when you try to access a nil member.

That is, Lights2[3] doesn't exist.

Additionally, using indirect access like that nullifies the point of using a generic for in the first place:

Lights1 = game.Workspace.LightSet1.Lights:GetChildren()
Lights2 = game.Workspace.LightSet2.Lights:GetChildren()

function onClick(click)
    for i,v in pairs(Lights1) do
       v.Color = Color3.new(0, 0, 127/255)
    end
    for i,v in  pairs(Lights2) do
        v.Color = Color3.new(0, 0, 127/255)
    end
       script.Parent.Material = Enum.Material.Neon
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

0
Ah, thanks so much! Moderance 30 — 9y
Ad

Answer this question