Does anyone know how can i check if a player is already wearing an accessory?

So i am making an image button that when activated it clones an accessory from the Replicated Storage to the players character but i do not know if there is a function or anything to check that or how could i do it…

1 Like

You can :FindFirstChildWhichIsA() to detect if an instance has the certain classname. Here’s how you can detect if a character has an accessory:

if Character:FindFirstChildWhichIsA("Accessory") then
    -- character has an accessory
else
    -- character does not have an accessory
end
2 Likes

But how would it know its an specific accessory for example if the player already has a hat accessory on then?

1 Like

You can try looking for the hat attachment inside of the hat. It can work like this:

repeat task.wait() until Player.CharacterApperanceLoaded

for _, Hat in pairs(Character:GetChildren()) do
	if Hat:IsA("Accessory") then
		for _, HatAttachment in pairs(Hat:GetDescendants()) do
			if HatAttachment:IsA("Attachment") and HatAttachment.Name == "HatAttachment" then
				-- HatAttachment.Parent.Parent is a hat
			end
		end 
	end
end
1 Like

I do not know why but it started making things worse… :frowning:

1 Like

Try adding repeat task.wait() until Player.CharacterApperanceLoaded at the beginning of the code, to ensure the player stuff has fully loaded. (I also updated it)

From experience, this is a bad way to do it. Use Player.CharacterAppearanceLoaded:Wait().

2 Likes

I know, but for some reason, I tried it in a local script, and my script never continued. (I used print debugging)

So i do not think you are getting me like since i gave you very little info but here is my script:

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

script.Parent.MouseButton1Click:Connect(function()
	local mask = game.ReplicatedStorage["Mask"]
	mask:Clone().Parent = char
	
end)

What i wanted to do is to check if the character already has an accessory named Mask and if so remove it from the players character. Like a wear accessory button.

This is inside of a image button by the way…

Oh ok, that makes everything more simple. Try this instead:

if char:FindFirstChild("Mask") then
    char:FindFirstChild("Mask"):Destroy()
else
    local mask = game.ReplicatedStorage["Mask"]
	mask:Clone().Parent = char
end
2 Likes

Thanks mate it worked perfectly :slight_smile:

1 Like

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