How to make a hat GamePass?

I’m gonna make a gamepass like if you buy it you will have instantly have the hat

Someone can help me?
Heres my script

local MarketplaceService = game:GetService(‘MarketplaceService’)

local GAMEPASSID = 000000

local function AddHatToCharacter(Character)
– Run code to add hat.
script:FindFirstChildOfClass(‘Accessory’):Clone().Parent = Character
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, GamePassId, Purchased)
if Purchased then – They actually bought it, and didn’t click ‘Cancel’
if GamePassId == GAMEPASSID then – It’s the hat gamepass
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)

local MarketplaceService = game:GetService(‘MarketplaceService’)

local GAMEPASSID = 000000

local function AddHatToCharacter(Character)
– Run code to add hat.
script:FindFirstChildOfClass(‘Accessory’):Clone().Parent = Character
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, GamePassId, Purchased)
if Purchased then – They actually bought it, and didn’t click ‘Cancel’
if GamePassId == GAMEPASSID then – It’s the hat gamepass
Player.CharacterAdded:Connect(function(Character)
AddHatToCharacter(Character)
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)

I want to see code easy.

lua does not use

if true
--code

Ok What should i do then?Thanks for helping me!

local MarketplaceService = game:GetService("MarketplaceService")
local GAMEPASSID = 000000
function AddHatToCharacter( chr )
	local newHat = x--here is reference proto newHat
	newHat.Parent= chr
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, GamePassId, Purchased)
	if Purchased then
		if GamePassId==GAMEPASSID then
			AddHatToCharacter(Player.Character)
		end
	end
end)
game.Players.PlayerAdded:Connect(function(Player)
	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GAMEPASSID) then
		Player.CharacterAppearanceLoaded:Connect(function(chr)
			AddHatToCharacter(chr)
		end)
	end
end)

its ok
the key is

 Player.CharacterAppearanceLoaded
2 Likes

Ok Thanks! :slight_smile: :slight_smile: :slight_smile:

local MarketPlaceService = game:GetService('MarketplaceService')
local GamepassId = 0

game.Players.PlayerAdded:Connect(function(PlayerWhoJoined)
	PlayerWhoJoined.CharacterAdded:Connect(function()
		if MarketPlaceService:UserOwnsGamePassAsync(PlayerWhoJoined.UserId, GamepassId) then
			local Cloned_Item = script:FindFirstChild('Accessory'):Clone()
			Cloned_Item.Parent = PlayerWhoJoined.Character
		end
	end)
	MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(Player,GamePassId,Purchased)
		if GamePassId == GamepassId and Purchased == true then
			local Cloned_Item = script:FindFirstChild('Accessory'):Clone()
			Cloned_Item.Parent = PlayerWhoJoined.Character
		end
	end)
end)

Tell me if this works.

1 Like

Have question Do I put my hat in the script and what do i do with the x I am a little new to scripting

Where do i put the hat? :slight_smile:

In the ServerScriptService
[30chars]

The Hat in severscriptservice?

[30Chars]

No, Put the Script in the ServerScriptStorage and put the Hat under that script.

Oh ok i get it now :wink: CHARRRSS

It works Thanks!

[30Chars]]]]]]]]]]

2 Likes