How to make a add stats on purchase thing

Hey fellow developers! I have been scripting for about 2 months now and was wondering, how to make a add stats on purchase thing.

Such as, if a player purchases a dev product it will add 500 coins to their stats. If you can possibly give me a link or a script I could use? This would be on a gui, not a part or a model.

Thanks!

but how would i add the stats?

This is how I do it:

In the local script, which is inside the purchase button:

script.Parent.MouseButton1Click:Connect(function()
     game:GetService("MarketplaceService"):PromptPurchase(game.Players.LocalPlayer, productId)
 end)

Then create a script, so we can hook this prompt with the server.

game:GetService("MarketplaceService").ProcessReceipt = function(reciept)  --Here we are hooking the client code with the server
      for _, player in pairs(game.Players:GetPlayers()) do  --Then we are looping through all the players in-game
          if player.UserId == reciept.PlayerId and receipt.ProductId == productId then  --If the player id is the same id of the player that purchased the product then we continue
              --Add here where it add to player's stats
          end  
      end
end

Adding to stats:

    player.leaderstats["CurrencyNameHere"] += 0 -- Amount that you want to give player

This is how I do it, hopefully this helps :slight_smile:

Can’t you just use GetPlayerByUserId rather than looping through every player?

2 Likes

This is how I do it, but what you say might save some lines.
Either way the code is executed the same way.

Thanks!! (30 charactersssssss)

Use the Players:GetPlayerByUserId(). It is less lines of code and better than using a loop. The larger a code is the more storage it takes

1 Like