Shapes disappearing after a bit?

I am making a button where when you click it, it makes shapes appear and then after you re click it, it makes them disappear. But the thing is, it works but it just disappears after a bit and I dont understand why, any help is appreciated!
(Heres my code: (I know its messy :sweat_smile: ))

local shapes = game.Workspace.Shapes
local on = false

local CyanSphere = shapes.CyanSphere
local YellowCube = shapes.YellowCube
local RedCube = shapes.RedCube

local GreenCube = shapes.GreenCube
local CyanCylinder = shapes.CyanCylinder
local BrownSphere = shapes.BrownSphere

local PinkCircle = shapes.PinkCircle
local DarkBlueCircle = shapes.DarkBlueCircle
local PurpleCuboid = shapes.PurpleCuboid

CyanSphere.Transparency = 1
CyanSphere.CanCollide = false

YellowCube.Transparency = 1
YellowCube.CanCollide = false

RedCube.Transparency = 1
RedCube.CanCollide = false

GreenCube.Transparency = 1
GreenCube.CanCollide = false

CyanCylinder.Transparency = 1
CyanCylinder.CanCollide = false

BrownSphere.Transparency = 1
BrownSphere.CanCollide = false

PinkCircle.Transparency = 1
PinkCircle.CanCollide = false

DarkBlueCircle.Transparency = 1
DarkBlueCircle.CanCollide = false

PurpleCuboid.Transparency = 1
PurpleCuboid.CanCollide = false

script.Parent.MouseButton1Click:Connect(function()
	if on == false then
		
		CyanSphere.Transparency = 0
		CyanSphere.CanCollide = true

		YellowCube.Transparency = 0
		YellowCube.CanCollide = true

		RedCube.Transparency = 0
		RedCube.CanCollide = true

		GreenCube.Transparency = 0
		GreenCube.CanCollide = true

		CyanCylinder.Transparency = 0
		CyanCylinder.CanCollide = true

		BrownSphere.Transparency = 0
		BrownSphere.CanCollide = true

		PinkCircle.Transparency = 0
		PinkCircle.CanCollide = true

		DarkBlueCircle.Transparency = 0
		DarkBlueCircle.CanCollide = true

		PurpleCuboid.Transparency = 0
		PurpleCuboid.CanCollide = true
		
		on = true
		
	elseif on == true then
		
		CyanSphere.Transparency = 1
		CyanSphere.CanCollide = false

		YellowCube.Transparency = 1
		YellowCube.CanCollide = false

		RedCube.Transparency = 1
		RedCube.CanCollide = false

		GreenCube.Transparency = 1
		GreenCube.CanCollide = false

		CyanCylinder.Transparency = 1
		CyanCylinder.CanCollide = false

		BrownSphere.Transparency = 1
		BrownSphere.CanCollide = false

		PinkCircle.Transparency = 1
		PinkCircle.CanCollide = false

		DarkBlueCircle.Transparency = 1
		DarkBlueCircle.CanCollide = false

		PurpleCuboid.Transparency = 1
		PurpleCuboid.CanCollide = false
		
		on = false
		
	end
end)

You can simplify your if-statements slightly:

if not on then -- Same as if on == false
	...
else -- Since on wasn't false, it must be true. Therefore equal to if on == true
	...
end

Try adding print("Show") between these lines:

and add print("Hide") between these lines:

Hmmmm can’t it be something to do with the parts then, and not the script?

Ok I’ll try the else instead of elseif and let you know but the Hide and Show, It does not run the Hide function because when they did disappear, I had to click it twice (once to make the value as true to turn it off then false to turn it back on) and @ohmarcell how would it be something with the parts? becuase I already set them all false and CanCollide = false on the start and in the start of the script.

If canCollide is false and anchored is false, then they could have fallen out of the world

Nono the thing is they do spawn for a bit but then they just POOF disappear (yes they are anchored tho)

Disappear as in they are gone from the explorer? Or as in they go invisible?

Try implementing this rewrite, it’s a lot cleaner and may function better.
(Assuming you aren’t trying to filter some parts from others, then I could rewrite it for that purpose if you want)

local Shapes = game.Workspace.Shapes:GetChildren()

local On = false

for i, v in pairs(Shapes) do
	v.Anchored = true
	v.Transparency = 1
	v.CanCollide = false
end

script.Parent.MouseButton1Click:Connect(function()
	if On then
		On = false
		for i, v in pairs(Shapes) do
			v.Transparency = 1
			v.CanCollide = false
		end
	else
		On = true
		for i, v in pairs(Shapes) do
			v.Transparency = 0
			v.CanCollide = true
		end
	end
end)

When I tried to go in testing, I spawned in the shapes and they didnt disappear. I did test out some stuff but then my original script also started working somehow? If this happens again I will repost on this post but anyways thank you so much for helping me guys I appreciate it alot! :grinning: