Gamepass Purchase Handling Help

Hello, I’m wondering how should I process a purchase on the server with this local script structure.

for _, imageLabel in pairs(WeaponsListFrame:GetChildren()) do
    local button = imageLabel:FindFirstChildWhichIsA("TextButton")
    if button then
        local buttonType = button:GetAttribute("Type")
        local btnName = button.Name
        
        if buttonType == "Coins" then
            button.MouseButton1Click:Connect(function()
                shopEvent:FireServer(btnName)
            end)
        elseif buttonType == "Robux" then
            button.MouseButton1Click:Connect(function()
                local gamepassId = ShopConfig[btnName].gamepassId
                MarketPlaceService:PromptGamePassPurchase(player, gamepassId)
                print(gamepassId)
            end)
        end
    end
end

When I had single gamepass I used this on server

local gamepassId = 1391743106

Players.PlayerAdded:Connect(function(player)
    local hasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
    
    if hasGamepass then
        print(player.Name .. "Has gamepass with id" .. gamepassId)
    end
end)

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, id, purchaseSuccessful)
    if purchaseSuccessful and id == gamepassId then
        print(player.Name .. "Has bought gamepass" .. id)
    end
end)
1 Like

Just check if a user owns each gamepass individually and then also check if they bought it. Doesn’t seem like you’re experiencing and issue really. They way you would change it on the server depends on your game

1 Like

So, just use this on server ?

Players.PlayerAdded:Connect(function(player)
    local hasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
    
    if hasGamepass then
        print(player.Name .. "Has gamepass with id" .. gamepassId)
    end
end)
1 Like

What, no! You would also have to use MarketplaceService.PromptGamePassPurchaseFinished on the server to see if they bought it while playing.

1 Like

Alright, but how should I get gamepass id on server ? I’m using ModuleScript to store values

local ShopConfig = {
	SlapTool = {
		TypeOfPrice = "Robux",
		gamepassId = 1391743106
	},
	
	BananaPeel = {
		TypeOfPrice = "Coins",
		price = 50
	}
}

return ShopConfig
1 Like

Good. On the server, require the module and then do something like

local ShopConfig = require(path.to.module)

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, id, purchaseSuccessful)
    if not purchaseSuccessful then return end
    local pass = nil
    for _, item in pairs(ShopConfig) do
        if item.gamepassId == id then
            pass = item
            break
        end
    end
    if not pass then warn("Couldn't process gamepass purchase") return end

    print(`Player {player.Name} bought gamepass with id: {item.gamepassId} ({item.TypeOfPrice})`)
end)
2 Likes