Problem with low graphics button

So, I am working on this low graphics button that disabled all materials and changes them to the “Smooth Plastic” material. There is one problem though, I am getting an error of Invalid value for enum Material. I cannot figure out how to fix this error, any ideas? I will provide my code below so that it is easier for you to assist me with my issue.

Code

local Enabled = false
local Button = script.Parent

for i, part in pairs(workspace:GetDescendants()) do
	if part:IsA("BasePart") then
		table.insert(Materials, i, part.Material)
	end
end

Button.MouseButton1Click:Connect(function()
	for _, part in pairs(workspace:GetDescendants()) do
		if part:IsA("BasePart") and Enabled == false then
			Enabled = true
			Button.Text = "LOW"
			part.Material = Enum.Material.Plastic
		elseif Enabled == true then
			for i, part in pairs(workspace:GetDescendants()) do
				if part:IsA("BasePart") then
					Enabled = false
					Button.Text = "HIGH"
					part.Material = Materials[i]
				end
			end
		end
	end
end)
1 Like

What’s the “Materials” though?

Nevermind, it’s just the Materials table, I’m gussing.

1 Like

Do you have any solution on how to fix this?

This video might help.

you never check if Materials[i] is nil, so you sometimes are setting part.Material to nil which gives