on = false
script.Parent.ClickDetector.MouseClick:Connect(function()
for i,v in pairs(script.Parent.Parent:GetChildren()) do
if on == false then
v.Transparency = 1
on = true
elseif on == true then
v.Transparency = 0
on = false
end
end
end)
This is likely because you’re not screening for BaseParts: Try this
local on = false
script.Parent.ClickDetector.MouseClick:Connect(function()
local children = script.Parent.Parent:GetChildren()
if (not on) then -- If on == false
for i, v in next, children do
if (v:IsA("BasePart")) then
v.Transparency = 1
end
end
else
for i, v in next, children do
if (v:IsA("BasePart")) then
v.Transparency = 0
end
end
end
on = not on -- Invert the value of on
end)
local v = 1
script.Parent.ClickDetector.MouseClick:Connect(function()
for i,o in pairs(script.Parent.Parent:GetDescendants()) do
if o:IsA("BasePart") then
o.Transparency = v
end
end
if v == 1 then v = 0 else v = 1 end
end)
Adding onto this, I’ve managed to simplify my code.
local on = false
script.Parent.ClickDetector.MouseClick:Connect(function()
for i, v in next, script.Parent.Parent:GetChildren() do
if (v:IsA("BasePart")) then
v.Transparency = (not on) and 1 or 0 -- Ternary Statement
end
end
on = not on
end)
I don’t see any reason to here unless @FV107Scimitar’s model structure necessitates it. You can see in the original code that :GetDescendants() is not used. (It should really only be used if needed, it’s quite a bit more resource intensive than :GetChildren())