Script not detecting accesorys

This script is supposed to detect if a player has accessorys and change their material yet it doesn’t work.
I’ve tried to look on the devforum for help but found nothing.

game:GetService("ReplicatedStorage").GhostEvent.OnServerEvent:Connect(function(player)
	player:LoadCharacter()
	player.Character:SetAttribute("Died",true)
	player.Character:SetAttribute("Hidden",true)
	player.Character:SetAttribute("CantHide",true)
	for i, v in pairs(player.Character:GetChildren()) do
		if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then
			v.Material = Enum.Material.ForceField
			v.BrickColor = BrickColor.new("Medium stone grey")
		elseif v:IsA("Accessory") then
				v:WaitForChild("Handle").Material = Enum.Material.ForceField
				v:WaitForChild("Handle").BrickColor = BrickColor.new("Medium stone grey")
			end
		end
        end)
1 Like

I have script which clones all player Accessories, maybe it will help you:

local Players = game:GetService("Players")

local function applyAppearance(plr)
	local UserID = plr.UserId
	local appearance = Players:GetCharacterAppearanceAsync(UserID)

	-- Wait for the character to be fully loaded
	local char = plr.Character or plr.CharacterAdded:Wait()
	char:WaitForChild("Humanoid") -- Ensure Humanoid is loaded

	-- Apply the character appearance
	for _, item in ipairs(appearance:GetChildren()) do
		local itemClone = item:Clone()
		itemClone.Parent = char
	end
end

Players.PlayerAdded:Connect(function(plr)
	-- Apply appearance when player joins
	plr.CharacterAdded:Connect(function()
		applyAppearance(plr)
	end)
end)
1 Like

If you want to make ALL character different color or material you should use :GetDescendants() and you need to look for mesh inside accesory’s handle and remove its texture then it will apply color and material sometimes.

1 Like

Also im warning you that material will not apply sometimes by this method!

1 Like

Hello!

I’m not sure what’s wrong there but I can give you some suggestions.

	elseif v:IsA("Accessory") then
				v:WaitForChild("Handle").Material = Enum.Material.ForceField
				v:WaitForChild("Handle").BrickColor = BrickColor.new("Medium stone grey")
			end

What you are doing here is, you are checking if the v is an accessory, and then you are trying to change its material or BrickColor, and it seems like you are assuming you always have a child named “Handle” and then changing its Material and brickColor like you did to the baseparts.

What I would do as well as have done was this, that takes cares of all mesh parts that may be in an accessory:

Here’s my example, try this way
(Example, Server Script):

	elseif v:IsA("Accessory") then
         print("Passed: 0")
          for i2, v2 in ipairs(v:GetDescendants()) do
               if v2:IsA("MeshPart") or v2:IsA("BasePart") or v2:IsA("Part") then
                 print("Passed: 1")
                 v2.Material = Enum.Material.ForceField
                 v2.BrickColor = BrickColor.new("Medium stone grey")
              end
          end
    end

You can either get the descendants or children of the Accessory V and then check if the descendant or child returned by the table is a meshPart then execute the remainder.

This is what I have done in the past, accessories usually hold meshParts, and even Parts sometimes, but Meshparts often.

Have you tried validating the code by putting a print statement as you see above? I wrote a print statement “Passed: 0”, in my above example.

Also wondering why you’ve chosen to use WaitForChild as it is not needed on a server Script usually.

1 Like

Use GetDescendants instead

game:GetService("ReplicatedStorage").GhostEvent.OnServerEvent:Connect(function(player)
	player:LoadCharacter()
	player.Character:SetAttribute("Died",true)
	player.Character:SetAttribute("Hidden",true)
	player.Character:SetAttribute("CantHide",true)
	for i, v in player.Character:GetDescendants() do
		if not v:IsA("BasePart") then continue end
		v.Material = Enum.Material.ForceField
		v.BrickColor = BrickColor.new("Medium stone grey")
	end
end)
1 Like

Same result, only changes the player’s body’s material and color, not the accessory.

Same result, only changes the player’s body’s material and color, not the accessory. (It doesn’t even print anything in the output)

1 Like

Did you removed the mesh texture first?

1 Like

The script doesn’t run at all, no matter if the texture is removed or not. It just changes the player’s body color and material and then stops.

I found a solution, try this!

game:GetService("ReplicatedStorage").GhostEvent.OnServerEvent:Connect(function(player)
	player:LoadCharacter()
	player.CharacterAdded:Once(function()
		task.wait(0.1)
		player.Character:SetAttribute("Died",true)
		player.Character:SetAttribute("Hidden",true)
		player.Character:SetAttribute("CantHide",true)
		for i, v in pairs(player.Character:GetDescendants()) do
			if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then
				v.Material = Enum.Material.ForceField
				v.BrickColor = BrickColor.new("Medium stone grey")
			elseif v:IsA("Accessory") then
				v:WaitForChild("Handle").Material = Enum.Material.ForceField
				v:WaitForChild("Handle").BrickColor = BrickColor.new("Medium stone grey")
				v:WaitForChild('Handle'):FindFirstChildOfClass('SpecialMesh').TextureId = "rbxassetid://"
			end
		end
	end)
end)
1 Like

That changes material and color for every Part and Accesorry.

1 Like

Now it just doesn’t change anything at all.

did my solution not work too? . . . . . .

1 Like

Sorry to bump, but this can’t be feasible, it is not convenient to first check if the child is a “BasePart” and if it isn’t moving on as it will ignore anything else that is a MeshPart or so and so.

But if you do something like this, then that works too:

game:GetService("ReplicatedStorage").GhostEvent.OnServerEvent:Connect(function(player)
	player:LoadCharacter()
	player.Character:SetAttribute("Died",true)
	player.Character:SetAttribute("Hidden",true)
	player.Character:SetAttribute("CantHide",true)
	for i, v in player.Character:GetDescendants() do
		if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("Part") then 
		v.Material = Enum.Material.ForceField
		v.BrickColor = BrickColor.new("Medium stone grey")
       end
	end
end)

If wondering about the continue, it’s not needed since we will anyway get to the “end” of the iteration and move on.

1 Like

No, same as waffle’s first idea.

The script works perfectly fine for me

1 Like

No it wouldnt, a Mesh part is a Base Part

the continue is used to make the code more readable and not nest if statements

1 Like

the script works perfectly fine, it’s mostly like the problem with firing the RemoteEvent
image

1 Like

I’ll check if the remote event has some issues