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

Y dis gui lightswitch no work? ;-;

Asked by 8 years ago
local isOn = true
parts = script.Parent.Parent.Lights:GetChildren()
function on()
    isOn = true
    for i, v in pairs(parts) do
        if v:IsA("Part") then
            light = v:GetChildren() -- Cant find problem?
            light.Enabled = true
        end
    end
end

function off()
    isOn = false
        for i, v in pairs(parts) do
        if v:IsA("Part") then
            light = v:GetChildren()
            light.Enabled = false
        end
    end
end

function onClicked()
    if isOn == true then off() else on() end
end

script.Parent.MouseButton1Down:connect(onClicked)

Can't find problem, so I need help whats wrong with this script for the light (ik im horrible at scripting you say)

1 answer

Log in to vote
4
Answered by 8 years ago

Firstly, you're not horrible in scripting. In this website, people are here to help you, not to ridicule you. You should look in the output window. There's a lot you can see from there, that will help you in the long run.

local isOn = true
parts = script.Parent.Parent.Lights:GetChildren()
function on()
    isOn = true
    for i, v in pairs(parts) do
        if v:IsA("Part") then
            light = v:GetChildren() -- here
            light.Enabled = true -- here
        end
    end
end

So, firstly, I have marked the 2 points where it should error. numero uno should still work, since you're only defining a variable, but numero dos (i take french) shouldn't. The reason why is that GetChildren will return a table of objects. So if we did something like...

local parts = game.Workspace:GetChildren()

-- we would get something like...

parts = {workspace.Part, workspace.Baseplate}

So how would we fix this? Well, we could use a pairs loop to check all the children and enable it. I'm going to assume that all the children have pointlights in them, so it's safe to do this.

local isOn = true
parts = script.Parent.Parent.Lights:GetChildren()
function on()
    isOn = true
    for i, v in pairs(parts) do
        if v:IsA("Part") then
            light = v:GetChildren() -- Cant find problem?
            for _,t in pairs(light) do    
            t.Enabled = true
        end
        end
    end
end

Now that we have that done, we can do the same thing to the next.

function off()
    isOn = false
        for i, v in pairs(parts) do
        if v:IsA("Part") then
            light = v:GetChildren()
        for _,w in pairs(light) do
                    w.Enabled = false
        end
        end
    end
end

function onClicked()
    if isOn == true then off() else on() end
end

script.Parent.MouseButton1Down:connect(onClicked)

There we go, both functions are rewritten. So if we wanted to put this all together then....

local isOn = true
parts = script.Parent.Parent.Lights:GetChildren()
function on()
    isOn = true
    for i, v in pairs(parts) do
        if v:IsA("Part") then
            light = v:GetChildren() -- Cant find problem?
            for _,t in pairs(light) do    
            t.Enabled = true
        end
        end
    end
end

function off()
    isOn = false
        for i, v in pairs(parts) do
        if v:IsA("Part") then
            light = v:GetChildren()
        for _,w in pairs(light) do
                    w.Enabled = false
        end
        end
    end
end

function onClicked()
    if isOn == true then off() else on() end
end

script.Parent.MouseButton1Down:connect(onClicked)

I am going to assume that all parts have a light inside of them. It will still work otherwise, because pairs loops don't break on error.

0
Thank you! With all the explanation with what I did wrong, This really helped me with scripting and learn a more about :GetChildren(). supermarioworld323 45 — 8y
0
#longanswers supermarioworld323 45 — 8y
Ad

Answer this question