Basically when I buy a gamepass from clicking a button, this script is ran twice, I’m only charged once though. Any ideas why this might be?
MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(loserPlayer, itemId, was_purchased)
if was_purchased then
print("GAMEPASS PURCHASED")
local productInfo = MarketPlaceService:GetProductInfo(itemId, Enum.InfoType.GamePass)
local itemPrice = productInfo.PriceInRobux
local creator = productInfo.Creator
loserPlayer.leaderstats.Donated.Value += itemPrice
local playerId = loserPlayer.UserId
local itemPriceString = tostring(itemPrice)
local loserName = loserPlayer.Name
local Message = "[SYSTEM]: " .. loserName .. " donated " .. itemPriceString .. " ROBUX to " .. creator.Name .. "!"
local colour = Color3.fromRGB(38, 212, 212)
donatedChatEvent:FireAllClients(colour, Message)
-- Check if the winner and loserPlayer exist
if workspace:FindFirstChild(creator.Name) and workspace:FindFirstChild(loserPlayer.Name) then
-- Check if the DonationEffect and Purchase sound exist
if game:GetService("ServerStorage").Effects:FindFirstChild("DonationEffect") and workspace.Sounds:FindFirstChild("Purchase") then
local purchaseEffect = game:GetService("ServerStorage").Effects.DonationEffect.Attachment:Clone()
purchaseEffect.Parent = workspace[creator.Name].Head
local sound = workspace.Sounds.Purchase
local sound1 = sound:Clone()
sound1.Parent = workspace:FindFirstChild(loserPlayer.Name).Torso
sound1:Play()
sound1.Ended:Once(function()
sound1:Destroy()
end)
task.wait(2.5)
if purchaseEffect then
purchaseEffect:Destroy()
end
else
print("DonationEffect or Purchase sound not found in the game.")
end
else
print("Winner or loserPlayer not found in the workspace.")
end
else
return
end
return
end)
Thank you, what if the player deletes the gamepass and buys it again? If I stored it in a datastore then when the player purchases it again they wont get the items
You can use :UserOwnsGamepassAsync and then have it stored only for this session in some cache so they can purchase it again after deleting it and rejoining. Just be wary of this
Or as an alternative just implement a cooldown that automatically adds that player with the gamepass to the list and clear it after some time have passed
Something like this
--before the event
local purchasedPasses = {}
--when the event triggers
purchasedPasses[gamepassID] = purchasedPasses[gamepassID] or {} --Create a new table for gamepass if none exist
table.insert(purchasedPasses[gamepassID], player.UserId)
task.delay(5, function()
table.remove(purchasedPasses[gamepassID], table.find(purchasedPasses[gamepassID], player.UserId)
end)
And then just check if the player id is in that table when the event fires
Thanks for your help, I changed your code as it was getting an error, I thought this would work but still hasnt stopped the code from running twice. What have I done wrong with my code?
local purchasedPasses = {}
MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(loserPlayer, itemId, was_purchased)
if was_purchased then
if table.find(purchasedPasses, itemId) then
return
else
table.insert(purchasedPasses, itemId)
task.delay(5, function()
table.remove(purchasedPasses, table.find(purchasedPasses, itemId))
end)
local productInfo = MarketPlaceService:GetProductInfo(itemId, Enum.InfoType.GamePass)
local itemPrice = productInfo.PriceInRobux
local creator = productInfo.Creator
loserPlayer.leaderstats.Donated.Value += itemPrice
local playerId = loserPlayer.UserId
local itemPriceString = tostring(itemPrice)
local loserName = loserPlayer.Name
local Message = "[SYSTEM]: " .. loserName .. " donated " .. itemPriceString .. " ROBUX to " .. creator.Name .. "!"
local colour = Color3.fromRGB(38, 212, 212)
donatedChatEvent:FireAllClients(colour, Message)
-- Check if the winner and loserPlayer exist
if workspace:FindFirstChild(creator.Name) and workspace:FindFirstChild(loserPlayer.Name) then
-- Check if the DonationEffect and Purchase sound exist
if game:GetService("ServerStorage").Effects:FindFirstChild("DonationEffect") and workspace.Sounds:FindFirstChild("Purchase") then
local purchaseEffect = game:GetService("ServerStorage").Effects.DonationEffect.Attachment:Clone()
purchaseEffect.Parent = workspace[creator.Name].Head
local sound = workspace.Sounds.Purchase
local sound1 = sound:Clone()
sound1.Parent = workspace:FindFirstChild(loserPlayer.Name).Torso
sound1:Play()
sound1.Ended:Once(function()
sound1:Destroy()
end)
task.wait(2.5)
if purchaseEffect then
purchaseEffect:Destroy()
end
else
print("DonationEffect or Purchase sound not found in the game.")
end
else
print("Winner or loserPlayer not found in the workspace.")
end
end
else
return
end
return
end)
Just found the issue, my function that detects the purchase was inside another function. Everytime this function was called it “stacked” up the promptgamepassfinished functions. So it was like I had two of them. Thanks for your help