Click on UI button, every material of each part in the game changes

I got every descendant of every part of workspace, and made its material smoothplastic.
image
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?

You should make a table with all the parts and their original material and istead of making a loop you should make a game.DescendantAdded event

That’s a MUCH better idea, also how would I put the instance, and the original material in the table?

You would set the index as the instance and the value as it’s material :

list[part] = part.Material
1 Like

Like this.

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

Edit: Added comments

1 Like

Thanks a lot, I’ll take a look at it so it’s not just straight up copy and paste without learning anything

I’ll add some comments to help you understand it better.

No I understand it, it’s fine. It’s not complex or anything

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.