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

for i,v in pairs not working?

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

✓ Accepted answer

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)

1 comment

0

Ah, thanks so much!

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