No errors coming up when ran. The hat goes into the script as a child. Hat is an accessory with handle named 'Accessory'.
local MarketplaceService = game:GetService('MarketplaceService')
local GAMEPASSID = 5885163
local function AddHatToCharacter(Character)
script:FindFirstChildOfClass('Accessory'):Clone().Parent = Character
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, GamePassId, Purchased)
if Purchased then
if GamePassId == GAMEPASSID then
Player.CharacterAdded:Connect(function(Character)
AddHatToCharacter(Character)
end)
if Player.Character then
AddHatToCharacter(Player.Character)
end
end
end
end)
game:GetService('Players').PlayerAdded:Connect(function(Player)
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GAMEPASSID) then
Player.CharacterAdded:Connect(function(Character)
AddHatToCharacter(Character)
end)
end
end)

6 comments
Awesome! It does work, but the hat sits too high on the players head. Why would this be?
this would be the accessories position
It doesn't work! I've tried different methods to correspond, but nothing seems to make it work with the accessory in Replicated storage.
try using an already made hat/hair and putting the handle in the accessory of said hat/hair
What do you mean exactly?
If you insert a character model with hair you can use the accessory that hair uses to have an accessory already positioned so that the hair wont float