Enum.Material.Marble is not a valid member of "Enum.Material"

when ever i run this code i just get the error of Enum.Material.Marble is not a valid member of “Enum.Material” even though it is
is this just a glitch or is my code broken?

im running this through a gui button

local lastMat = {}
game.ReplicatedStorage.material.Event:Connect(function()
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("BasePart") then
			table.insert(lastMat, tostring(v.Material))
			v.Material = Enum.Material.SmoothPlastic
		end
	end
end)
game.ReplicatedStorage.material2.Event:Connect(function()
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("BasePart") then
			v.Material = Enum.Material[lastMat[i]]
		end
	end
end)
2 Likes

Maybe just add the whole enum value to the table instead of using it’s name.

table.insert(lastMat, v.Material)

Then in the other event do it like this:

v.Material = lastMat[i]

now its saying
“Invalid value for enum Material”

after further research i found it out

Share your solution! I was curious as to how you solved it, and it could serve as a resource for future readers with the same problem

From the code you provided, it looks like the issue might be that the values you insert into lastMat won’t be guaranteed to line up the same way when you read from them in the other event handler. The table.insert function adds table elements sequentially, but when you iterate through a list of descendants and only operate on items that are BaseParts you are basically ‘skipping’ indices, so there might not be anything at that index in the table.

If you want to keep track of a part’s previous material you should consider doing something like mapping the material directly to it’s part, i.e.

local lastMat = {}
game.ReplicatedStorage.material.Event:Connect(function()
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("BasePart") then
			lastMat[v] = v.Material
			v.Material = Enum.Material.SmoothPlastic
		end
	end
end)
game.ReplicatedStorage.material2.Event:Connect(function()
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("BasePart") and lastMat[v] then
			v.Material = lastMat[v]
		end
	end
end)

Or you could make use of the new attribute feature for instances.

game.ReplicatedStorage.material.Event:Connect(function()
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("BasePart") then
			v:SetAttribute("PreviousMaterial", v.Material)
			v.Material = Enum.Material.SmoothPlastic
		end
	end
end)
game.ReplicatedStorage.material2.Event:Connect(function()
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("BasePart") then
			local mat = v:GetAttribute("PreviousMaterial")
			if mat ~= nil then
				v.Material = mat
			end
		end
	end
end)
1 Like