Changing Material in Accessories

Hello! As the title clearly says, I’m trying to figure out a way to change the MATERIAL, not the texture but the MATERIAL of an accessory.

I’ve searched far and wide, only to find no article that seems to talk about this, which is kind of weird.

Is there anyway I can change the material on a player’s accessory via script? Or is it really not possible to do so?

1 Like

Yep so if you wanted to through a script you could iterate through every single hat a player has on them.

-- SERVICES
local Players = game:GetService("Players")

-- VARIABLES
local DesignatedMaterial = "ForceField"

-- FUNCTIONS
local function characterAdded(character)
	for _, accessory in pairs(character:GetChildren()) do
		if accessory:IsA("Accessory") then
			local handle = accessory:FindFirstChildWhichIsA("BasePart")
			if handle then
				local material
				
				local success, err = pcall(function()
					material = Enum.Material[DesignatedMaterial]
				end)

				if not success or not material then
					warn("Invalid material:", DesignatedMaterial)
					return
				else
					handle.Material = material
					print("Set material for accessory:".. accessory.Name)
				end
			end
		end
	end 
end

local function playerAdded(player)
	characterAdded(player.Character or player.CharacterAdded:Wait())
	player.CharacterAdded:Connect(characterAdded)
end

-- RUNTIME
for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(playerAdded)(player)
end

Players.PlayerAdded:Connect(playerAdded)

Put this script into serverscriptservice, or wherever you handle characterloading.

4 Likes

How would you remove the texture of the accessory?

Use this loop:

-- SERVICES
local Players = game:GetService("Players")

-- VARIABLES
local DesignatedMaterial = "ForceField"

-- FUNCTIONS
local function characterAdded(character)
	for _, accessory in pairs(character:GetChildren()) do
		if accessory:IsA("Accessory") then
			local handle = accessory:FindFirstChildWhichIsA("BasePart")
			if handle then
				local material
				
				if handle:IsA("MeshPart") then
					handle.TextureID = ""
				else
					handle:FindFirstChildOfClass("SpecialMesh").TextureId = ""
				end
				
				local success, err = pcall(function()
					material = Enum.Material[DesignatedMaterial]
				end)

				if not success or not material then
					warn("Invalid material:", DesignatedMaterial)
					return
				else
					handle.Material = material
					print("Set material for accessory:".. accessory.Name)
				end
			end
		end
	end 
end

local function playerAdded(player)
	characterAdded(player.Character or player.CharacterAdded:Wait())
	player.CharacterAdded:Connect(characterAdded)
end

-- RUNTIME
for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(playerAdded)(player)
end

Players.PlayerAdded:Connect(playerAdded)

What we did was add these lines:

				if handle:IsA("MeshPart") then
					handle.TextureID = ""
				else
					handle:FindFirstChildOfClass("SpecialMesh").TextureId = ""
				end

This checks to see if the handle is a meshpart, because sometimes they are if you are using custom accessories if I recall correctly. Then we just override the textureid and set it to nothing.

2 Likes

It works great, though I do have a concern.

Say I were to change the material into something like Slate or Concrete. How would that work when it comes to SpecialMeshes?

Replying to this to get some attention rahhh

Don’t do that, you will get an discourse email about necrobumping a topic. It happened to me too!

Oh! I did not know, thanks! :sweat_smile:

I only did it so I could get some assistance, this thread has been dead for a while.

1 Like

Not sure, but you can just use the code and edit the chosenmaterial to “Slate” and do some experimenting!

1 Like

Alright, thank you so much for the help!

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