Giving inventory after buying gamepass

Can you show the error itself?

1 Like

Hey, my computer is glitchy so I cant send screenshots but I can tell you:

local MarketplaceService = game:GetService("MarketplaceService")

local gamePassID = 0000000 -- Change this to your game pass ID

local function Bought(player, purchasedPassID, purchaseSuccess)

if purchaseSuccess == true and purchasedPassID == gamePassID then

local jackhammer = Game.ServerStorage.Jackhammer:Clone()

jackhammer.Parent = player.Backpack

end

end

local giveOnRespawn(player) -- The 'player' part underlines.

if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)

local jackhammer = Game.ServerStorage.Jackhammer:Clone() 

jackhammer.Parent = player.Backpack

end

end

game.Players.PlayerAdded:Connect(player)

player.CharacterAdded:Connect(function(character)

giveOnRespawn(player)

end)

end)

MarketplaceService.PromptGamePassPurchaseFinished:Connect(Bought)

The error itself btw
16:22:02.267 - ServerScriptService.BuyJackhammer:13: Incomplete statement: expected assignment or a function call

1 Like

Try this approach :))

--[[ SERVICES ]]
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local MarketplaceServices = game:GetService("MarketplaceService")
--[[ VARIABLES ]]
local Tools = ServerStorage:WaitForChild("DefineTheFolderOrTheToolHere")
--[[ FUNCTIONS ]]
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
           if MarketplaceServices:UserOwnsGamePassAsync(Player.UserId, 0) then
	       Tools["ToolNameHere"]:Clone().Parent = Player.Backpack
      end
  end)
end)
2 Likes

Ill test it rn, Ill tell you if it worked or not.

3 Likes

it’s not that hard, whenever a player joins, check if they have the gamepass, if they do, give them the gear when they spawn

local serverStorage = game:GetService("ServerStorage")
local tool = serverStorage.YourTool -- Your tool
local gamepassId = "000" -- Your gamepass id
local players = game:GetService("Players")
local marketPlaceService = game:GetService("MarketPlaceService")

players.PlayerAdded:Connect(function(player)
    local success, hasPass = pcall(marketPlaceService.UserOwnsGamePassAsync, marketPlaceService, player.UserId, gamepassId)
    if success and hasPass then
        -- Player has gamepass
        player.CharacterAdded:Connect(function(character)
           local backpack = player:WaitForChild("Backpack")
           if not backpack:FindFirstChild(tool.Name) then
               local toolClone = tool:Clone()
               toolClone.Parent = backpack;
           end           
        end)
    end
end)
2 Likes

I’m 100% sure from this script, I just made some edits!

local MarketplaceService = game:GetService("MarketplaceService")
 
local gamePassID = 0000000  -- Change this to your game pass ID
 
local function Bought(player, purchasedPassID, purchaseSuccess)
 
	if purchaseSuccess == true and purchasedPassID == gamePassID then
        local jackhammer = Game.ServerStorage.Jackhammer:Clone()
        jackhammer.Parent = player.Backpack
	end
end

local giveOnRespawn(player)
   if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) then
      local jackhammer = Game.ServerStorage.Jackhammer:Clone()
      jackhammer.Parent = player.Backpack
   end
end
 
game.Players.PlayerAdded:Connect(player)
    player.CharacterAdded:Connect(function(character)
       giveOnRespawn(player)
   end)
end)
MarketplaceService.PromptGamePassPurchaseFinished:Connect(Bought)
2 Likes

A script in ServerScriptService.

the line local giveOnRespawn(player) doesn’t work. I put it in the TextButton where i’d purchase it.

You should put this on a script in ServerScriptService, it gives the player the tool if they own the gamepass when they respawn.

1 Like

wait, how do I fire it? And should it be a localscript?

Immediately after buying the gamepass, you will need to give the desired item to the player. You could do this with the PromptGamePassPurchaseFinished event:

local marketplaceService = game:GetService("MarketplaceService")

local gamepassId = 0000000 -- Change the value to your gamepass's id

local jackhammer = game:GetService("ServerStorage"):WaitForChild("Jackhammer")

local function onGamepassPurchaseEnded(player, gamepassId, purchased)
   if purchased == true then
      local clone = jackhammer:Clone()

      clone.Parent = player.Backpack
   end
end

marketplaceService.PromptGamePassPurchaseFinished:Connect(onGamepassPurchaseEnded)

You will also need to give the tool to the player once it rejoins so you can use the UserOwnsGamePassAsync function in the MarketplaceService:

local marketplaceService = game:GetService("MarketplaceService")

local gamepassId = 0000000 -- Change this to your gamepass's id

local jackhammer = game:GetService("ServerStorage"):WaitForChild("Jackhammer")

game:GetService("Players").PlayerAdded:Connect(function(player)
    if marketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) == true then
       local clone = jackhammer:Clone()

       clone.Parent = player.Backpack
    end
end)

As for giving the tool to the player when it respawns, you can use the CharacterAdded event from the Player instance:

local marketplaceService = game:GetService("MarketplaceService")

local gamepassId = 0000000 -- Change this to your gamepass's id

local jackhammer = game:GetService("ServerStorage"):WaitForChild("Jackhammer")

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        if marketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) == true then
           local clone = jackhammer:Clone()

           clone.Parent = player.Backpack
        end
    end)
end)

The script should be in a seperate script called “GamepassPurchasesHandler” in ServerScriptService.

Full script:

local marketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")

local gamepassId = 0000000 -- Change the value to your gamepass's id

local jackhammer = game:GetService("ServerStorage"):WaitForChild("Jackhammer")

local function onGamepassPurchaseEnded(player, gamepassId, purchased)
   if purchased == true then
      local clone = jackhammer:Clone()

      clone.Parent = player.Backpack
   end
end

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(function()
        if marketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) == true then
           local clone = jackhammer:Clone()

           clone.Parent = player.Backpack
        end
    end)
end

marketplaceService.PromptGamePassPurchaseFinished:Connect(onGamepassPurchaseEnded)
players.PlayerAdded:Connect(onPlayerAdded)
1 Like

No, you just put it in a script and the function fired when the player is added.

guys you are all wrong about the script yall are trying here lemme show you my script. (Put the jackhammer in serverStorage.)

local MarketplaceService = game:GetService(“MarketplaceService”)
local Players = game:GetService(“Players”)

local gamePassID = 1 --Your id here

function Spawned(player)

local HasGamepass = false

local success, message = pcall(function()
	HasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
end)

if not success then
	warn("Checking In Player Has Gamepass"..tostring(message))
	return
end

if HasGamepass == true then
	game.ServerStorage["Jackhammer"]:Clone().Parent = player.Backpack
end

end

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
Spawned(player)
end)
end)

Players.PlayerAdded:Connect(Spawned)

This should work for you! Try it out!

1 Like

In serverScriptService? I am assuming this would go there.

Script 1:

function GiveTool(Player, Character)
	local AlreadyHasTool = false
	for i,v in pairs(Player.Backpack:GetChildren()) do
		if v.Name == "Tool" then
			AlreadyHasTool = true
		end
	end
	for i,v in pairs(Character:GetChildren()) do
		if v.Name == "Tool" then
			AlreadyHasTool = true
		end
	end
	if not AlreadyHasTool then
		local Tool = game.ServerStorage:WaitForChild("Tool"):Clone()
		Tool.Parent = Player.Backpack
	end
end


game.ServerScriptService.ProcessReceipt.AddProduct:Fire(GAMEPASSID, function(receiptInfo, Player)
	local _Character = Player.Character
	if _Character then
		local Tool = game.ServerStorage:WaitForChild("Tool"):Clone()
		Tool.Parent = Player.Backpack
	end
	Player.CharacterAdded:Connect(function(Character)
		GiveTool(Player, Character)
	end)
end)


game.Players.PlayerAdded:Connect(function(Player)
	local Attempts = 0
	local success = false
	local err = nil
	local PlayerHasGamePass = false
	repeat
		Attempts += 1
		success, err = pcall(function()
			PlayerHasGamePass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, GAMEPASSID)
		end)
		if not success then wait(5) end
	until
		success or Attempts > 3
	if PlayerHasGamePass and success then
		Player.CharacterAdded:Connect(function(Character)
			GiveTool(Player, Character)
		end)
	end
end)

Script 2 (ProcessReceipt)

warn("Process Receipt Callback Will Be Assigned In This Script Instance.")
local productFunctions = {}


script.AddProduct.Event:Connect(function(productId, func)
	local success, err = pcall(function()
		productFunctions[productId] = func
	end)
	if not success then
		warn("(ProcessReceipt) Error while adding product id: "..productId or -1)
		print("\nError: "..err)
	end
end)
script.RemoveProduct.Event:Connect(function(productId)
	local success, err = pcall(function()
		productFunctions[productId] = nil
	end)
	if not success then
		warn("(ProcessReceipt) Error while removing product id: "..productId or -1)
		print("\nError: "..err)
	end
end)


local function processReceipt(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	local success, result = pcall(productFunctions[receiptInfo.ProductId], receiptInfo, player)
	if not success or not result then
		warn("(ProcessReceipt) Error occurred while processing a product purchase")
		print("\nProductId:", receiptInfo.ProductId)
		print("\nPlayer:", player)
		print("\nError/Result: "..result)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end


game:GetService("MarketplaceService").ProcessReceipt = processReceipt

ProcessReceipt should have 2 BindableEvents inside of it, “AddProduct” and “RemoveProduct”

1 Like

Yes in ServerScriptService so you are correct.

Let me know if it works for you!

Hey, So I am currently modifying it to match everything to my UI names. I will get done with that soon and hope to tell you the result.