Tool not equipped when gamepass bought

  1. What do you want to achieve? I want a tool to go to the players backpack when the player buys a gamepass.

  2. What is the issue? I don’t know! The output and script analysis did not find anything wrong, and neither have I.

  3. What solutions have you tried so far? Figuring it out.

Here is my script:

script.Parent.MouseButton1Click:Connect(function()
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 9960300

function Spawned(player)

local HasGamepass = false

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

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

if HasGamepass == true then
game.ServerStorage.BoomBox:Clone().Parent = player.Backpack
end
end

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

script.Parent is a button. boombox is stored in serverstorage. I used a server script for this

2 Likes

If you want it to happen when the player buys the gamepass then you should use the PromptGamePassPurchaseFinished event.

and then give the player their item if the third parameter is true.

That entire script is only going to run when you click the button. I suggest placing the Spawned function and it’s accompanying PlayerAdded function below the MouseButton1Click function.

1 Like

script.Parent.MouseButton1Click:Connect(function()

local plr = game.Players.LocalPlayer

local button = script.Parent

local MarketPlaceService = game:GetService(“MarketplaceService”)

MarketPlaceService:PromptGamePassPurchase(plr,9960300)

end)

local gamePassID = 9960300

function Spawned(player)

local HasGamepass = false

local MarketPlaceService = game:GetService(“MarketplaceService”)

local sucess, message = pcall(function()

HasGamepass = MarketPlaceService:UserOwnsGamePassAsync(player.userID, gamePassID)

end)

if not sucess then

warn(“Checking In Player Has Gamepass”…tostring(message))

return

end

if HasGamepass == true then

game.ServerStorage.BoomBox:Clone().Parent = player.Backpack

end

end

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

player.CharacterAdded:Connect(function()

Spawned(player)

end)

end)

Something like this?

Later you prompt the gamepass purchase you do the event

marketPlace.PromptPurchaseFinished:Connect(function(thePlayer, theGamepassID, isPurchased)

so it takes 3 parameters which would be the player that finished the prompt, the ID of the gamepass of the prompt and the bool that checks if the player bought it or not

isPurchased would be the bool so you check if its true, and if that really is true then you give the item to the player

Yeah but i already put if HasGampass== true then (rest of the code)

But it only works once. That is when the player joins, if the player buys while playing then he wont get the item

script.Parent.MouseButton1Click:Connect(function()

local plr = game.Players.LocalPlayer

local button = script.Parent

local MarketPlaceService = game:GetService(“MarketplaceService”)

MarketPlaceService:PromptGamePassPurchase(plr,9960300)

end)

local gamePassID = 9960300

function Spawned(player)

local HasGamepass = false

local MarketPlaceService = game:GetService(“MarketplaceService”)

local sucess, message = pcall(function()

MarketPlaceService.PromptPurchaseFinished:Connect(function(thePlayer, theGamepassID, isPurchased)

if not sucess then

warn(“Checking In Player Has Gamepass”…tostring(message))

return

end

if HasGamepass == true then

game.ServerStorage.BoomBox:Clone().Parent = player.Backpack

end

end

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

player.CharacterAdded:Connect(function()

Spawned(player)

end)

end)

end)
It dosent work, there is an error in the output

You haven’t set HasGamepass to true in the script so it won’t clone the boombox to the backpack of the player, if the pcall has not failed then make sure to set HasGamepass to true.

So I would do:

I would recommend you to learn more about the marketplace service and about events.

What i said is the third parameter which is “isPurchased”, thats the bool value and you need to check if its true.

If its true it means that the player actually bought the gamepass when the prompt appeared

And you need to do what Tamzy3D said too