How do I find a specific accessory a player has equipped?

I’m making a datastore where a hat that you get in-game gets saved as a string on equip, and when you join that hat is loaded onto you by name from the string by searching the hats folder (game.ServerStorage.Hats). The problem is that the player could already have accessories equipped from their roblox avatar, and that makes it a lot harder to find the hat from in-game. How can I grab the accessory from in-game?

i dont exactly know how you setup the reloading of the accesory but, before you clone the hat check if the hat exists on the character, if it does, remove it

1 Like

Try this one …

--> SeverScript @ServerScriptService
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)

        --however you got to character, this is how you can do that check 
		for _, accessory in ipairs(character:GetChildren()) do
			if accessory:IsA("Accessory") and
				accessory.AccessoryType == Enum.AccessoryType.Hat then
				print("Player is wearing a hat: ", accessory.Name)
			end
		end

	end)
end)

No I didn’t finish this off as I have no clue of how your database is set up.
You should have no problems finishing this and doing the actual checking.
This is getting you to accessory.Name … that’s all you need.

2 Likes

that doesnt solve the problem of finding the exact hat

1 Like

So basically you want to find/check a hat within a player character that matches the one in game/datastore?

1 Like

exactly

for example: if player has “Hat1” equipped, the datastore goes into the character and saves Hat1 specifically and not any other accessory they may have equipped

1 Like

Sorry for the wait I was doing something but here:

local HatsStore = game.ServerStorage.Hats

local function GetHat(Player:Player) 
	local FoundHat = nil
	
	for I, X in pairs(Player.Character:GetChildren()) do
		local IsAccesory = X:IsA("Accessory") 
		local PossibleFind = HatsStore:FindFirstChild(X.Name)
		
		if IsAccesory and PossibleFind then
			if X.AccessoryType == PossibleFind.AccessoryType then
				FoundHat = X
                break
			else
				continue
			end
		else
			continue
		end
	end
	
	return FoundHat
end

Edit: I want to clarify this function may return nil so check if it does find the hat or not. In addition, I’m not sure if the player can wear multiple accessories so if they do let me know and I’ll update the code

1 Like

Try using a folder inside StarterCharacterScripts and then use the folder as your in-game hat storage. Should be really easy to find your hat even if the player has already equipped lots of hats.

1 Like

can you explain how this would work? the hats folder has multiple hats in it and looking at your code it looks like youd only be able to find one of the hats
i may be wrong so thats why im asking

2 Likes

Yea I mentioned it here… I didn’t know if a player could wear multiple hats or not. And are the hats just in the folder or are they further organized into sub-folders?

1 Like

theyre all just in the folder
also you can only wear one hat at once, theyre just all stored in that folder

1 Like

I forgot about the explanation part my bad. All it does is loop through the character’s children to check if the instance is an accessory and whether their name matches a hat in the hats folder. After validating that the instance is an accessory and it matches the name of another hat in the hats folder, it will do a final check to see if the accessory types are the same. After, it will set FoundHat to the accessory found, break out the loop, and then return the FoundHat

1 Like

If that’s the case then the function will work for you. The function will find the hat and then return it to you.

ill try it out when im home, ill lyk if it works

worked, thank you for the help (although i needed to tweak it a little)

It’s great to hear it worked for you! I’m a little bit curious, what did you tweak? I believed it would work flawlessly that’s why

i just needed to do some extra things to make it compatible with the datastore