So I made a script that gives a gamepass technically just this:
local plr = game.Players.LocalPlayer
local id = blah
script.Parent.MouseButton1Down:Connect(function()
game.MarketplaceService:PromptGamePassPurchase(plr, id)
end)
Now I need to know, how would I make it so it gives an item when you buy it?
What I am looking for is when you join the game, it gives it.
I am also looking for when you buy it, it immediately gives it once the transaction is successful.
Any ideas on how do give a certain tool after buying the gamepass?
You can use MarketplaceService’s PromptGamepassPurchaseFinished event, it is pretty simple to be used like this:
local function gamepassPurchaseFinished(player, gamepassId, wasPurchased)
--Here you may match gamepassId with the one you want to, and check if it wasPurchased and do the reward for the passed player.
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)
So I know this, but the main thing I am confused with is now how would it maybe clone the jackhammer (The gamepass item which is located in Game.ServerStorage.Jackhammer), to the starterpack, would I just do:
local function gamepassPurchaseFinished(player, gamepassId, wasPurchased)
Game.ServerStorage.Jackhammer:Clone()
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)
So now I have bought the gamepass, cloned the jackhammer now how will I put this jackhammer into my hand so I can equipt it?
local function gamepassPurchaseFinished(player, gamepassId, wasPurchased)
local jackhammer = Game.ServerStorage.Jackhammer:Clone()
jackhammer.Parent = Game.StarterPack
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)
StarterPack is not a valid member of Game so it will cause an error. I would suggest this(If it will work):
local function gamepassPurchaseFinished(player, gamepassId, wasPurchased)
local jackhammer = Game.ServerStorage.Jackhammer:Clone()
jackhammer.Parent = player:WaitForChild("StarterPack")
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)
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)
Correct me if I’m wrong but, shouldn’t you connect the function giveOnRespawn when the character has respawned using the CharacterAdded event? This function will only run once the player is in the game. Meaning that it will run only once.