So I was trying to make a script on how to make it so when I click a part with the a clickdetector it would set the parts transparency to 0 but if I click it again the transparency will be 1 again.
local P = script.Parent
local Parts1 = P.Parent.Open:GetChildren()
local Parts2 = P.Parent.Shut:GetChildren()
local roofOpen = false
P.ClickDetector.MouseClick:connect(function()
for i,v in pairs(Parts1) do
v.Transparency = (roofOpen and 1.0 or 1.0)
end
for i,v in pairs(Parts2) do
v.Transparency = (roofOpen and 0.0 or 0.0)
end
roofOpen = not roofOpen
end)```
Hi, the use of changing boolean variables in and/or statements along with constants is often not necessary and can make readability difficult (at least for me). Here’s how I would write it:
local P = script.Parent
local Parts1 = P.Parent.Open:GetChildren()
local Parts2 = P.Parent.Shut:GetChildren()
local roofOpen = false
P.ClickDetector.MouseClick:Connect(function()
if roofOpen then
for i,v in pairs(Parts1) do
v.Transparency = 1
end
for i,v in pairs(Parts2) do
v.Transparency = 0
end
else
for i,v in pairs(Parts1) do
v.Transparency = 0
end
for i,v in pairs(Parts2) do
v.Transparency = 1
end
end
roofOpen = not roofOpen
end)