My gamepasshandler isn't working

I was using TheDevKing’s tutorial on how to make a gamepassshopGUI however the issue is that the script doesn’t give the tool to the player for some reason even if I have the gamepass.

local MarketplaceSerivice = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local ToolGamepassId = 16269994

game.Players.PlayerAdded:Connect(function(player)

	local Success, message = pcall(function()
		hasPass = MarketplaceSerivice:UserOwnsGamePassAsync(player.UserId, ToolGamepassId)
	end)

	if hasPass then
		print("Player Has Gamepass")

		local Tool = game.ReplicatedStorage.RedNeonStick:Clone()
		Tool.Parent = player.Backpack
	end
end)

local function onPromptGamePassPurcahseFinised(player, purchasedPassId, purchaseSuccess)
	if purchaseSuccess == true and purchasedPassId == ToolGamepassId then
		print(player.Name .. "purchased the gamepass!")

		local Tool = game.ReplicatedStorage.RedNeonStick:Clone()
		Tool.Parent = player.Backpack
	end
end

MarketplaceSerivice.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurcahseFinised)

This is the script.

1 Like

Are you testing in studio or ingame?

I’m testing in studio.

Writing this cause of characters

Try testing in game as opposed to studio and see if you still have the issue, your code looks okay

It still doesn’t give the tool to my backpack

I think I see why now, your hasPass variable is local to the pcall, I think, try putting a variale declartion beforehand?

local hasPass

local Success, message = pcall(function()
	hasPass = MarketplaceSerivice:UserOwnsGamePassAsync(player.UserId, ToolGamepassId)
end)

if hasPass then
	print("Player Has Gamepass")

	local Tool = game.ReplicatedStorage.RedNeonStick:Clone()
	Tool.Parent = player.Backpack
end

Shall I put this before the event or inside?

I recommend inside of the event

The thing is that it detects that I have a gamepass as it outputs this. However, it just doesn’t place the tool into my backpack which is strange image

I think it could be that the character has to be loaded first? Before the pcall, add this line player.CharacterAdded:Wait()

1 Like

That suprisingly worked lol. I wonder why that was needed.

I think it could be that you added the tool to the backpack, the character gets added and requests all the tools in StarterGear, there was nothing, so no tools to give.

Oh and I recommend also parenting a tool into StarterGear so they’ll also have the tool when they respawn, so just change this

local Tool = game.ReplicatedStorage.RedNeonStick:Clone()
	Tool.Parent = player.Backpack

To this

local Tool = game.ReplicatedStorage.RedNeonStick
Tool:Clone().Parent = player.Backpack
Tool:Clone().Parent = player.StarterGear

If you have anymore issues don’t be afraid to make another post!

1 Like

Thanks man! I appreciate the help :ok_hand: :slightly_smiling_face:

1 Like

Anytime! I hope to be able to help you out in your future issues! Good luck with your project!

1 Like

One quick question, How would I go about looping through multiple tool gamepasses and then giving players the specific tools if they have the gamepasses for it.

What I did is have a folder in serverstorage with the gamepass tools in it, then in those tools, I add an Attribute called “GamepassId”, and in that attribute, I add in the gamepass Id of the gamepass that gives that tool if you own it,

Then in the code, when a player joins, I loop through the folder, check if they own the gamepass with the id mentioned in the attribute, clone the tool twice and put one in backpack and one in startergear

1 Like
local MarketplaceSerivice = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local SS = game:GetService("ServerStorage")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	for i,v in pairs(SS.GamepassTools:GetChildren()) do
		local hasPass
		local Success, message = pcall(function()
			hasPass = MarketplaceSerivice:UserOwnsGamePassAsync(player.UserId, v.GamepassId)
		end)

		if hasPass then
			print("Player Has Gamepass")

			local Tool = v
			Tool:Clone().Parent = player.Backpack
			Tool:Clone().Parent = player.StarterGear
		end
	end	
end)

I tried this and for some reason it’s not working D: please help lol

Is GamePassId an IntValue in th tool? If so, you forgot to put .Value after it

I used attributes like you mentioned before

That’s not how you get attributes, to get it you would do v:GetAttribute("GamepassId")

1 Like