How to figure out if a player has a certain hat

Hello there!

Me and my friend @3Escot are making a system to detect if a player has the “Doge” hat. And then to give them the rank. (Join game > check for hat > rank)
I have seen a group do this (Crown of Ooowners - Roblox)
How would we do this?

Greetings AvarageMilkEnjoyer

https://developer.roblox.com/en-us/api-reference/function/MarketplaceService/PlayerOwnsAsset

2 Likes
local players = game:GetService("Players")
local marketplace = game:GetService("MarketplaceService")
local playerOwnsAsset = marketplace.PlayerOwnsAsset

local function hasAsset(player, assetId, maxTries)
	maxTries = maxTries or 5
	if maxTries == 0 then return end
	maxTries -= 1
	
	local success, result = pcall(playerOwnsAsset, marketplace, player, assetId)
	if success then
		if result then
			return true
		else
			return false
		end
	else
		warn(result)
		task.wait()
		hasAsset(player, assetId, maxTries)
	end
end

local function onPlayerAdded(player)
	local hasDoge = hasAsset(player, 151784320)
	print(hasDoge) --false
end

players.PlayerAdded:Connect(onPlayerAdded)

This will work for any asset but I’ve used the doge accessory’s asset ID to showcase. For me, hasDoge is false as I do not own the doge hat.

1 Like

well are you talking about owning the hat or wearing the hat??

1 Like

This post is 3 years old, why’d you bump it?

1 Like