How to script a button that removes all materials and restores then when clicked again

I was wondering how to script a button that removes all textures to make the game less laggier similar to the feature on Roblox Studio. I’ve looked everywhere on DEVFORUM but I haven’t found anything yet. Does anyone have any methods to accomplish this?

3 Likes

Hmmm, first search result when searching up your question.

https://devforum.roblox.com/t/how-to-make-a-button-that-disables-roblox-textures/690614
(On here they don’t exactly answer your question, but some answers can be easily modified to answer it.)

The script work but how would I make it work on the click of the button, I tried this but it didn’t work. It just shows an error.

script.Parent.MouseButton1Click:connect(function()
while wait(.1) do
for i, v in pairs(game.Workspace:GetDescendants()) do
if v:IsA(“BasePart”) then
v.Material = Enum.Material.SmoothPlastic
end
end
end

What error are you getting in the output?

script.Parent.MouseButton1Down:Connect(function()    
for _, object in ipairs(workspace:GetDescendants()) do
      if object:IsA("Texture") then
        object:Destroy()
      end
    end
end)

Sorry, not textures but the base part materials, how do I destroy those.

Replace "Texture" with "BasePart" in his code.

It deletes the parts instead of the materials

You can’t delete the material of a BasePart. It always has to be set to something. As per the example in the link, it changes it to one of the materials that doesn’t take much processing room.

1 Like

You cannot remove the materials from a BasePart, as they are a property.
What you can do is make them a smooth, solid color by doing this.

script.Parent.MouseButton1Down:Connect(function()    
for _, object in ipairs(workspace:GetDescendants()) do
      if object:IsA("BasePart") then
        object.Material = Enum.Material.SmoothPlastic
      end
    end
end)
2 Likes

Use a for loop to cycle trough all objects in workspace, if its a texture, destroy it.

Sorry to bug you but how do I write a code to revert it when clicked again.

Save the current value before changing it, then when the button is clicked again, apply the previous value.

e.g.

local currentTexture

local db = false
local partDb = false

script.Parent.MouseButton1Down:Connect(function()    
for _, object in ipairs(workspace:GetDescendants()) do
      if object:IsA("BasePart") then
         if not partDb then -- so this part only runs once 
              partDb = true
              currentTexture = object.Material -- save the current material before changing it
         end
        if db == false then
             object.Material = Enum.Material.SmoothPlastic
              db = true
         else
             object.Material = currentTexture
             db = false
        end
      end
    end
end)

You would have to store it somewhere.

local smooth = false
script.Parent.ClickDetector.MouseClick:Connect(function()
	for _, object in ipairs(workspace:GetDescendants()) do
		if object:IsA("BasePart") then
			if smooth and object:FindFirstChild("OriginalMaterial") then
				object.Material = object.OriginalMaterial.Value
				object.OriginalMaterial:Destroy()
			elseif not smooth then
				local originalMaterial = Instance.new("StringValue")
				originalMaterial.Name = "OriginalMaterial"
				originalMaterial.Value = object.Material.Name
				originalMaterial.Parent = object
				object.Material = Enum.Material.SmoothPlastic
			end
		end
	end
	smooth = not smooth
end)

This script will work in a part with a ClickDetector. You can easily modify it to work in a GUI.
Keep in mind, LocalScripts will not replicate to anyone else without using remotes and a normal Script.

8 Likes

Thanks for all the help :slight_smile:

Not much of a scripter so I wouldn’t know how to do that, lol.

I would personally do this with a table.

local realMaterials = {}
for i,v in ipairs(workspace:GetChildren()) do
	if v:IsA("BasePart") then
		realMaterials[v.Name] = v.Material
	end
end


local function turnIntoPlastic()
	for i,v in ipairs(workspace:GetChildren()) do
		if v:IsA("BasePart") then
			v.Material = Enum.Material.SmoothPlastic
		end
	end
end

local function turnBack()
	for i,v in ipairs(workspace:GetChildren()) do 
		if v:IsA("BasePart") then
			v.Material = realMaterials[v.Name]
		end
	end
end

Either way works.

3 Likes

Personally, instead of using

if object:IsA("BasePart") then
    --code
end

I would use

if not object:IsA("BasePart") then continue end
--code

there isn’t much of a difference here but I think its cleaner.

Necro-reply much?
Yeah, I agree though. What you just replied with is definitely something I’d do these days.

Why are you using continue instead of return here?