Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Hat Gamepass Script not functioning, why?

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)

1 answer

✓ Accepted answer

First of all you can try putting the accessory inside of ReplicatedStorage and then clone it into the player from there if they have the gamepass:

local Accessory = game:GetService("ReplicatedStorage"):FindFirstChildOfClass("Accessory")
local Market = game:GetService("MarketplaceService")
local GPId = 5885163

local function AddAccessory(Char)
	local clone = Accessory:Clone()
	clone.Parent = Char
end

local function Gamepass_Purchased(Player, PassId, IfPurchased)
	if IfPurchased == true then
		if PassId == GPId then
			if Player.Character ~= nil then
				AddAccessory(Player.Character)
			end
			Player.CharacterAdded:Connect(function(Char)
				AddAccessory(Char)
			end)
		end
	end
end

local function CheckPass(Player, PassId)
	if Market:UserOwnsGamePassAsync(Player.UserId, PassId) then
		repeat wait() until Player.Character ~= nil
		if Player.Character ~= nil then
			AddAccessory(Player.Character)
		end
		Player.CharacterAdded:Connect(function(Char)
			AddAccessory(Char)
		end)
	end
end

Market.PromtGamePassPurchaseFinished:Connect(Gamepass_Purchased)

game.Players.PlayerAdded:Connect(CheckPass)

This should work for you

6 comments

0

Awesome! It does work, but the hat sits too high on the players head. Why would this be?

0

this would be the accessories position

0

It doesn't work! I've tried different methods to correspond, but nothing seems to make it work with the accessory in Replicated storage.

0

try using an already made hat/hair and putting the handle in the accessory of said hat/hair

0

What do you mean exactly?

0

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

This discussion is preserved as it was. New answers, comments, and votes are closed.