Giving the Player a Hat if they own that hat on Roblox

I’m trying to make a script that gives the player the Plague Wormie hat if they own it on roblox, I am using a StarterCharacter and want only people who own the Plague Wormie to have it.

local ASSET_ID = 6203025104

local Players = game:GetService("Players")
local MPS = game:GetService("MarketplaceService")
local POS = MPS.PlayerOwnsAsset

Players.PlayerAdded:Connect(function(player)
	local Humanoid = player.Character.Humanoid
	local ServerStorage = game:GetService("ServerStorage")
	local Hat = ServerStorage.Miscellaneous:WaitForChild("Plague Wormie"):Clone()
	
	local success, doesPlayerOwnAsset = pcall(POS, MPS, player, ASSET_ID)
	if doesPlayerOwnAsset then
		print("They own it!")
		--Hat.Parent = player.Character
		Humanoid:AddAccessory(Hat)
	else
		print("They don't own it!")
	end
end)

This is my current code, but no matter what I search up or try it won’t give them the hat.

The script is located in Server Script Service

image
I get this error.

1 Like

Before anything else…

local ASSET_ID = 6203025104

local Players = game:GetService("Players")
local MPS = game:GetService("MarketplaceService")
local POS = MPS.PlayerOwnsAsset

Players.PlayerAdded:Connect(function(player)
	local Humanoid = player:WaitForChild("Character").Humanoid
	if Humanoid then
	local ServerStorage = game:GetService("ServerStorage")
	local Hat = ServerStorage.Miscellaneous:WaitForChild("Plague Wormie"):Clone()
	
	local success, doesPlayerOwnAsset = pcall(POS, MPS, player, ASSET_ID)
	if doesPlayerOwnAsset then
		print("They own it!")
		--Hat.Parent = player.Character
		Humanoid:AddAccessory(Hat)
	else
		print("They don't own it!")
	end
		print("How can we locate where head is if not Humanoid?")
	end
end)
1 Like

Didn’t seem to work, should I put it in a different place from Server Script Service? Also I’m using a StarterCharacter so I don’t know if I should change something because of it.

Yeah, ServerScriptService can’t see playerscript and maybe same goes to.

But move Hat from Server to Replicated first.

1 Like

I tried moving the script to StarterCharacterScripts to see if that could work, and also moved the hat to ReplicatedFirst but it still doesn’t seem to work, and I did make it check ReplicatedFirst.

I also tried changing Humanoid to = script.Parent.Humanoid to see if that could fix it.

Use HumanoidDescriptions
What you can do is get the description then change it a bit aka removing other accessories then hats then just apply it!

1 Like

No, ReplicatedStorage. Ah wait, someone has other
way.

1 Like

I looked a bit into HumanoidDescriptions, but couldn’t find a good way to use them as they output a string to my knowledge. If there is any documentation that demonstrates a good way to use it in that way please do link it.

Still doesn’t seem to work even after moving it to ReplicatedStorage.

Yep, official documentation is this. Has link to API documentation.

And this for layered Hat.

1 Like
game.Players.PlayerAdded:Connect(function(plr)
	local PlrHumanDesc = game.Players:GetHumanoidDescriptionFromUserId(plr.UserId)
	local HumanDesc = Instance.new("HumanoidDescription")
	HumanDesc.HatAccessory = PlrHumanDesc.HatAccessory
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	hum:ApplyDescription(HumanDesc)
end)

might want to change a few things in it ig or wait

1 Like

I thought of an idea and it ended up working but may not work for others as I have a pretty specific use case. I changed it up to just delete the hat from the avatar if they don’t own it.

local ASSET_ID = 6203025104

local Players = game:GetService("Players")
local MPS = game:GetService("MarketplaceService")
local POS = MPS.PlayerOwnsAsset

Players.PlayerAdded:Connect(function(player)
	local success, doesPlayerOwnAsset = pcall(POS, MPS, player, ASSET_ID) 
	if doesPlayerOwnAsset then
		print("They own it!")
	else
		print("They don't own it!")
		player.Character:FindFirstChild("Valiant Top Hat of Testing"):Destroy()
	end
	print("How can we locate where head is if not Humanoid?")
end)

I just remembered why I didn’t want to use HumanDescription, for my use I want to make the player wear the hat if they own it, not taking into consideration if they are wearing it just if they own the item. But thank you so much for trying to help me fix this issue, it means a lot.