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)
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)