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)
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)
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.
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, accessoriesusually 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.
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)
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)
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.