How can I make a setting that changes all materials to Smooth Plastic?

Hello there! I need to know how I can make a setting (in my game) that changes all materials to smooth plastic. The only solution for me right now is by doing it one by one in a script, but I can’t do that due to the amount of parts in my game!

I’m needing a solution to this, and if you’re asking, yes, this is for performance and it’s for an FNaF game.

3 Likes

Use a for loop on all decendants in the game, from there use a pcall to try and set the material which means that if it has no material value it wont stop due to the error you normally get and this will change absoloutly everything with a material to what you chose.

I may look dumb because I am, but I don’t know how to script this well. Can you give me a script example or is it not allowed? I’m not sure, I don’t know the DevForum so much and their guidelines. Are there specific arguments that call for specific parts or is it automatic to make all parts smooth plastic? Thanks!

not a problem, but make sure you check out the api for pcall() and for loops. Its very important to understand the code you use.

local Parts = game:GetDecendants()

for i = 1,#Parts do
    local Success, Error = pcall(function()
        Parts[i].Material = Enum.Materual.["Smooth Plasic"]
    end
    wait(LoopDelay)
end

Agreed. The resources we use in our games should be understood. So now, when I click the setting, it changes to smooth plastic, but how do I change it back? Is it specific or is there a history on a part?

You could save each part and their material to a dictionary before you change them. Then to change them back you just need to loop through that dictionary and set the material to whatever you saved.

1 Like

If you did it in studio command bar then just simply press CTRL-Z
Other wise store each part material in a loop.

llocal parts = game.workspace:GetDecendants()
local materials = {}

function changeToSmoothPlastic()
	for index, instance in next, parts do
		local yes,errorr = pcall(function()
			if instance:IsA("MeshPart") or instance:IsA("BasePart") or instance:IsA("Part") then
				table.insert(materials,{part = instance, material = instance.Material})
				instance.Material = Enum.Material.SmoothPlastic
			end
		end)
	end
end

function redoChanges()
	for index, info in next, materials do
		local yes, errorr = pcall(function()
			info.part.Material = info.material
		end)
	end
end

-- To make everything smooth plastic
changeToSmoothPlastic()

-- To change parts material to their previous material
redoChanges()
1 Like

https://developer.roblox.com/en-us/api-reference/function/Instance/GetDescendants

https://developer.roblox.com/en-us/api-reference/function/Instance/IsA

-- create a empty table
local materials = {}

-- Loop all descendants of workspace
for i, descendant in pairs(workspace:GetDescendants()) do
    -- If the descendent is not a basepart then skip this descendant
    if descendent:IsA("BasePart") == false then continue end
    -- save the part and material into the materials table
    materials[descendent] = descendant.Material
end

local function SetMaterials(material)
    -- Loop all saved parts
    for part, savedMaterial in pairs(materials) do
        -- Set the parts material to what was passed into this function or restore the material if nil was passed into the function
        part.Material = material or savedMaterial
    end
end

-- wait 20 seconds
task.wait(20)
-- set all parts to smooth plastic
SetMaterials(Enum.Material.SmoothPlastic)
-- wait 10 seconds
task.wait(10)
-- restore all the materials back to the original material
SetMaterials(nil)
3 Likes