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()
light.Enabled = true
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()
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()
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()
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.
2 comments
Thank you! With all the explanation with what I did wrong, This really helped me with scripting and learn a more about :GetChildren().
#longanswers