Model transparency help

I need help to make this go transparent when clicked and then not transparent, its a model with a few special meshes.

https://gyazo.com/57e5cf8052ac5339e8656ae2e0ffe586 this is the issue i keep getting

heres my code:

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)

Put this if elseif statement outside the loop, keep v.Transparency inside but move on outside as well

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)

Try this now

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)

Why you don’t use :GetDescendants()

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())

Thanks. I understand now I guess.