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

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

Asked by
traigla 75
9 years ago

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

Log in to vote
0
Answered by 9 years ago

Try this.

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

... rest of the script

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 traigla 75 — 9y
Ad
Log in to vote
0
Answered by
Sublimus 992 Moderation Voter
9 years ago

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)

Answer this question