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

(UNSOLVED) _,v in pairs suddenly not working?

This was working earlier, but suddenly stopped. Any help? I also get this error: 15:06:06.860 - SpotLight is not a valid member of SurfaceGui 15:06:06.861 - Script 'Workspace.MainLights.Script', Line 15 15:06:06.861 - Stack End 15:06:06.861 - Disconnected event because of exception

open = script.Parent.Open.Value
local lights = {}
lights = game.Workspace.MainLights:GetChildren()

function operate()
	if open == false then
		for _,v in pairs(lights) do
			v.SpotLight.Enabled = true
			script.Parent.BrickColor = BrickColor.new(37)
			open = true
			wait()
		end
	else
		for _,v in ipairs(lights) do
			v.SpotLight.Enabled = false
			script.Parent.BrickColor = BrickColor.new(21)
			open = false
			wait()
		end
	end
end

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

2 answers

Try this.

if open == false then
	for _,v in pairs(lights) do -- (changed in pairs to in ipairs)

... rest of the script

1 comment

0

No, still not working. If this helps, I get this error: 15:06:06.860 - SpotLight is not a valid member of SurfaceGui 15:06:06.861 - Script 'Workspace.MainLights.Script', Line 15 15:06:06.861 - Stack End 15:06:06.861 - Disconnected event because of exception

It is saying that Spotlight doesn't actually exist for each member of the table that is returned from ...MainLights:getChildren(). So, to fix this error, we need to check and see if a spotlight actually exists in each entry.

open = script.Parent.Open.Value
local lights = {}
lights = game.Workspace.MainLights:GetChildren()

function operate()
    if open == false then
        for _,v in pairs(lights) do
	    if v:findFirstChild("SpotLight") then -- Checks to see if a spotlight exists
            	v.SpotLight.Enabled = true
            	script.Parent.BrickColor = BrickColor.new(37)
            	open = true
            	wait()
	    end
        end
    else
        for _,v in ipairs(lights) do
	    if v:findFirstChild("SpotLight") then
            	v.SpotLight.Enabled = false
            	script.Parent.BrickColor = BrickColor.new(21)
            	open = false
         	wait()
	    end
        end
    end
end

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

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