Hat Gamepass only works for 1 hat

So I am currently working on making gamepasses add hats when purchased and so far it works for the first hat. However, when I duplicate the script for the second hat I get no error in the output but not only does the first hat not show up anymore, but the second hat doesn’t show up either. Is my script working and just doesn’t show in studio or am I do something wrong?

local MarketplaceService = game:GetService('MarketplaceService')

local GAMEPASSID = 9132723

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)


-- second hat


local GAMEPASSID = 9132741

local function AddHatToCharacter(Character)
	-- Run code to add hat.
    script:FindFirstChildOfClass('Accessory2'):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)
1 Like

I’m no expert, but in your function:
local function AddHatToCharacter(Character)
– Run code to add hat.
script:FindFirstChildOfClass(‘Accessory’):Clone().Parent = Character
end
It looks like you are only searching for the FirstChildOfClass(‘Accessory’) and cloning it. Wouldn’t that only select the first Accessory?

I forgot to change the second part of the code to find Accessory2 which I have as a different hat. It still doesn’t seem to work.

Seems like you aren’t checking for all the hats. You are only checking for one and only when the player joins.
Are these both in the same script?
Oh jeez, If they are then you are setting the variable GAMEPASSID to 2 different numbers and then trying to run 2 similar scripts with the same function names inside another script!

Oh yeah they are in the same game. Sorry I’m still relatively new to coding and I’m BURNT out from constant work. Could you lead me to a solution? I’ve tried using two scripts but it doesn’t seem to have had an impact. Should I change the variables?

I’m only a newbie at scripting as well.
I think you need to call the PlayerAdded function when a player joins the game and loop through the gamepasses to see if the player has any of them.
If they do, then call the AddHatToCharacter function and assign the correct hat there, if they don’t, move on through the list of gamepasses (if you do add more) until you get to the end of the list.
Just my 2 cents, and for sure I can’t write the code without making errors.

Yeah I’ve tried running a few checks and it still doesn’t seem to work. Lol this is really tilting me

Did you put
print(“stuff”)
lines in to see where the script is catching you up?

I actually figured it out on my own somehow. Could you explain what you mean by adding print. I know what it does and how it is used to check certain things but would I just put the print code in wherever I think it breaks?

Oh it’s sometimes used to find out what functions/loops etc your script is using if there’s no visible output.
You can print a value you are trying to use to see if it’s what you expect, or just something as simple as a print(“vehicle steers left”) if a car isn’t turning left, but you put that line where you think the code might be breaking to see where it actually is.

Is this all on one script, because I can see where your problem might be?

If its on one script,

First I think it was mentioned before, but your variable for the second hat is overwriting the variable for the first hat gamepass id. This is because they have the same name,
Next you might want to use FindFirstChild() instead, because you can search for the hat’s specific name.
Accessory2 isn’t a valid class, hats are only classed as Accessory.

Wouldn’t :FindFirstChild() be a more efficient way to go about rather than :FindFirstChildOfClass()?

local function AddHatToCharacter(Character)
   script:FindFirstChild("Hat1"):Clone().Parent = Character
   script:FindFirstChild("Hat2"):Clone().Parent = Character
end

The problem I’m seeing is that with :FindFirstChildOfClass() you are finding the first child that matches the criteria (in this case, if the class is an Accessory). So Hat1 gets cloned, but Hat2 doesn’t.