Making a switch loop

Hey, I am trying to make a switch that if you click it makes the part you clicked invisible and other parts visible, then if you click it again that parts go invisible and another one goes invisible

So I want it to loop like
Main, Second, Third, Main, Second, Third

So after the third click it switches back to the main part

--Variables

local clickdetector = script.Parent
local mainmodel = script.Parent.Parent.Parent
local chessmodel = mainmodel.Chess
local snakesmodel = mainmodel.Snakes
local traymodel = mainmodel.Tray

--Functions

--First Click (Makes tray invisible and chess one visible)
clickdetector.MouseClick:Connect(function()
	if traymodel.Tray0.Transparency == 0 then
		
		--Makes Tray Invisible
		traymodel.Tray0.Transparency = 1
		traymodel.Tray1.Transparency = 1
		traymodel.Tray2.Transparency = 1
		traymodel.Tray3.Transparency = 1
		traymodel.Tray4.Transparency = 1
		traymodel.Tray5.Transparency = 1
		
	if traymodel.Tray0.Transparency == 1 then
			
		--Makes Chess Visible
		chessmodel.Chess0.Transparency = 0
		chessmodel.Chess1.Transparency = 0
		chessmodel.Chess2.Transparency = 0
		chessmodel.Chess3.Transparency = 0
		chessmodel.Chess4.Transparency = 0
			
		end
	end
end)


--Second Click (Makes chess invisible and snakes one visible)
clickdetector.MouseClick:Connect(function()
	if chessmodel.Chess0.Transparency == 0 then

		--Makes Chess Invisible
		chessmodel.Chess0.Transparency = 1
		chessmodel.Chess1.Transparency = 1
		chessmodel.Chess2.Transparency = 1
		chessmodel.Chess3.Transparency = 1
		chessmodel.Chess4.Transparency = 1

	if snakesmodel.Snakes0.Transparency == 1 then

		--Makes Snakes Visible
		snakesmodel.Snakes0.Transparency = 0
		snakesmodel.Snakes1.Transparency = 0
		snakesmodel.Snakes2.Transparency = 0
		snakesmodel.Snakes3.Transparency = 0
		snakesmodel.Snakes4.Transparency = 0
		snakesmodel.Snakes5.Transparency = 0
		snakesmodel.Snakes6.Transparency = 0
			
		end
	end
end)


--Third Click (Makes snakes invisible and tray visible) (This one doesn't do anything for some reason)
clickdetector.MouseClick:Connect(function()
	if snakesmodel.Snakes0.Transparency == 0 then
		
		--Makes Snakes Invisible
		snakesmodel.Snakes0.Transparency = 1
		snakesmodel.Snakes1.Transparency = 1
		snakesmodel.Snakes2.Transparency = 1
		snakesmodel.Snakes3.Transparency = 1
		snakesmodel.Snakes4.Transparency = 1
		snakesmodel.Snakes5.Transparency = 1
		snakesmodel.Snakes6.Transparency = 1
		
		--Makes Tray Visible
		traymodel.Tray0.Transparency = 0
		traymodel.Tray1.Transparency = 0
		traymodel.Tray2.Transparency = 0
		traymodel.Tray3.Transparency = 0
		traymodel.Tray4.Transparency = 0
		traymodel.Tray5.Transparency = 0
	end
end)

I did the third click code but that just doesn’t do anything for some reason, I don’t get errors in the output

Maybe you can try this:

-- pseudo code

-- define variables...
local clickdetector = script.Parent
local mainmodel = script.Parent.Parent.Parent
local chessmodel = mainmodel.Chess
local snakesmodel = mainmodel.Snakes
local traymodel = mainmodel.Tray

local clickValue = 0

-- assuming trays were the first thing visible
clickdetector.MouseClick:Connect(function()
    clickValue = (clickValue % 3) + 1
    if clickValue == 1 then
        -- make trays invisible
        -- make chess visible
    elseif clickValue == 2 then
        -- make chess invisible
        -- make snakes visible
    elseif clickValue == 3 then
        -- make snakes invisible
        -- make trays visible
    end
end)

Edit: It does nothing, and it won’t do anything the way it is. I was too lazy put all the tray/chess/snake transparency changes in there…

1 Like

Okay Thank you I will try this

1 Like

It works thanks so much! I really appreciate the help

1 Like