I got every descendant of every part of workspace, and made its material smoothplastic.
My problem is how I’m gonna go about restoring the materials after pressing the button. Remote events and sending over information, or storing it in a table?
ALSO should I be doing a while loop every few minutes to set everything’s material to smoothplastic that isnt smooth plastic?
local button = script.Parent
local activated = false -- Whether or not the button is activated; are all materials set to SmoothPlastic?
local originalMaterials = {} -- Declare our table to store the original materials
local function descendantAdded(part) -- Declare our function to set a part to SmoothPlastic
if not activated then return end -- If the button is not activated, stop the function
if part:IsA("BasePart") then -- If the part is a BasePart (has a Material property)
originalMaterials[part] = part.Material -- Store our original material in the table. The index, or key, is the part we're changing, and the value is the part's original material. We do this before we set the material to SmoothPlastic
part.Material = Enum.Material.SmoothPlastic -- Set the material of the part
end
end
local function buttonActivated() -- Declare our function for when the button is activated
activated = not activated -- Switch the activated bool; setting it to the opposite of what it already is. So, if it were true, the opposite of true is false, so we'd be setting it to false
if activated then -- If the function is activated; if we're changing parts to SmoothPlastic
for _, part in ipairs(workspace:GetDescendants()) do -- Loop through the descendants of the workspace. Parts won't be anywhere but the workspace. We use _ for variables we won't use, in this case, it is the index, which we don't need, because it's just the part's numerical position in the array we got through workspace:GetDescendants().
task.defer(descendantAdded, part) -- Spawn a new thread to execute descendantAdded and pass the part as a parameter. This allows us to execute the function without yielding the rest of the current thread. (e.g. if we had a task.wait in the descendantAdded function, it wouldn't stop the for loop). Using task.defer instead of task.spawn for small performance boost
end
else -- If everything is already SmoothPlastic; setting everything back to original material
for _, part in ipairs(workspace:GetDescendants()) do -- Loop through workspace
if part:IsA("BasePart") then -- If the part is a BasePart
local originalMaterial = originalMaterials[part] -- Retrieve the part's original material from the table. Incase the material isn't set for some reason, it will return nil.
if originalMaterial then -- Checking if the material was actually there
part.Material = originalMaterial -- If so, set the part's material to the original
end
end
end
originalMaterials = {} -- After all that, reset the originalMaterials table
end
end
button.Activated:Connect(buttonActivated) -- Connection that fires when the button is activated
workspace.DescendantAdded:Connect(descendantAdded) -- Connection that fires when an object is added to the workspace