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 9 years ago
01local isOn = true
02parts = script.Parent.Parent.Lights:GetChildren()
03function on()
04    isOn = true
05    for i, v in pairs(parts) do
06        if v:IsA("Part") then
07            light = v:GetChildren() -- Cant find problem?
08            light.Enabled = true
09        end
10    end
11end
12 
13function off()
14    isOn = false
15        for i, v in pairs(parts) do
View all 27 lines...

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 9 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.

01local isOn = true
02parts = script.Parent.Parent.Lights:GetChildren()
03function on()
04    isOn = true
05    for i, v in pairs(parts) do
06        if v:IsA("Part") then
07            light = v:GetChildren() -- here
08            light.Enabled = true -- here
09        end
10    end
11end

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...

1local parts = game.Workspace:GetChildren()
2 
3-- we would get something like...
4 
5parts = {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.

01local isOn = true
02parts = script.Parent.Parent.Lights:GetChildren()
03function on()
04    isOn = true
05    for i, v in pairs(parts) do
06        if v:IsA("Part") then
07            light = v:GetChildren() -- Cant find problem?
08            for _,t in pairs(light) do   
09            t.Enabled = true
10        end
11        end
12    end
13end

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

01function off()
02    isOn = false
03        for i, v in pairs(parts) do
04        if v:IsA("Part") then
05            light = v:GetChildren()
06        for _,w in pairs(light) do
07                    w.Enabled = false
08        end
09        end
10    end
11end
12 
13function onClicked()
14    if isOn == true then off() else on() end
15end
16 
17script.Parent.MouseButton1Down:connect(onClicked)

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

01local isOn = true
02parts = script.Parent.Parent.Lights:GetChildren()
03function on()
04    isOn = true
05    for i, v in pairs(parts) do
06        if v:IsA("Part") then
07            light = v:GetChildren() -- Cant find problem?
08            for _,t in pairs(light) do   
09            t.Enabled = true
10        end
11        end
12    end
13end
14 
15function off()
View all 31 lines...

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 — 9y
0
#longanswers supermarioworld323 45 — 9y
Ad

Answer this question