You should put this on a script in ServerScriptService, it gives the player the tool if they own the gamepass when they respawn.
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)
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!
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”
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.
That sound wonderful! Hope it works out for ya!
I am about to test it, and just wondering, did it work for you?
It indeed did. That is why i showed you what script i was using but it only gives after resetting but it works tho!
One more thing, can I test this in studio (Test buying gamepass) or do I actually need to buy it?
You dont have to buy it. You can test it in studio. In my Team Creation studio when i press ‘Play’ in studio it says “You already own this item. Your account has not been charged.” but i tested on an alt if it worked and it does! But it is supposed to give an item tho right?
I am now heading to bed. But tomorrow (for me) im gonna check if it worked for you or not! So just reply to this and let me know!
If this one does not work, I will move onto your solution. But since when you purchase on studio, it is a test purchase, meaning it will not work in studio because my gamepass jackhammer is not actually being bought, it is just being test purchased meaning you do not actually ‘own’ it.
Exactly. But if you already own it, its gonna say ‘You already own this item. Your account has not been charged.’