Making a GUI toggleable (Toggle materials)

How would I make my GUI toggleable? I made a script that when you click a GUI the materials all turn to Smooth Plastic. I want it once it is enabled, you can click it again and all the materials go back to normal. How would I do this?

Script:

local toggled = true

script.Parent.SideMenu.ToggleMats.MouseButton1Click:Connect(function()
		for i,v in pairs(game.Workspace:GetDescendants()) do
			if v:IsA("UnionOperation") or v:IsA("MeshPart") or v:IsA("TrussPart") or v:IsA("Part") or v:IsA("BasePart") or v:IsA("CornerWedgePart") then
				v.Material = Enum.Material.SmoothPlastic
			end
		end
	end)

I really really need help! :frowning:

This should work

Code
local cache = {}

local function revertMaterial()
	for BasePart: BasePart, Material in pairs(cache) do
		BasePart.Material = Material
	end
end

local SmoothPlastic = Enum.Material.SmoothPlastic
local function changeToSmoothPlastic()
	for _, BasePart: BasePart in ipairs(game.Workspace:GetDescendants()) do
		if BasePart:IsA("BasePart") then
			cache[BasePart] = BasePart.Material
			BasePart.Material = SmoothPlastic
		end
	end
end


local ToggleMats = script.Parent.SideMenu.ToggleMats
local shouldRevert;
ToggleMats.MouseButton1Click:Connect(function()
	if not shouldRevert then
		changeToSmoothPlastic()
		shouldRevert = true
	else
		revertMaterial()
		shouldRevert = false
	end
end)
4 Likes