So, I'm working on an obby and I'm trying to make a skip stage GUI. But I searched it up on google and the model I got didn't work... I don't want to add teams, though. Here is the script I tried:
local productId = 47185810 -- Change to the ID of your developer product. local player = game.Players.LocalPlayer script.Parent.MouseButton1Click:connect(function() game:GetService("MarketplaceService"):PromptProductPurchase(player, productId) end) That script was in the GUI. stat = "Stage" -- Change to the stat's name. award = 1 -- Change to the ammount of stat to award.
MarketplaceService = Game:GetService("MarketplaceService") MarketplaceService.ProcessReceipt = function(receiptInfo) players = game.Players:GetPlayers() for i=1,#players do if players[i].userId == receiptInfo.PlayerId then players[i].leaderstats[stat].Value = players[i].leaderstats[stat].Value+award end end return Enum.ProductPurchaseDecision.PurchaseGranted end
This was in the serverscriptservice.
I got slightly confused but now I got it! Read from lines 1 to 5 for the LocalScript. Then for the rest (7 to end), it is the Server Script.
Okay, firstly, userId
is now deprecated, so use UserId
instead. Next, I would rather use for i, v in pairs(players) do
rather than for i = 1, #players do
when iterating over players, in my opinion. Finally, when using variables, make them local
so the script will run better. So... let me edit your script a little bit:
LocalScript:
local productId = 47185810 -- Change to the ID of your developer product. local player = game.Players.LocalPlayer script.Parent.MouseButton1Click:connect(function() game:GetService("MarketplaceService"):PromptProductPurchase(player, productId) end)
Server Script:
local id = 47185810 local stat = "Stage" -- Change to the stat's name. Make the variables LOCAL so the script will run better. local award = 1 -- Change to the ammount of stat to award. MarketplaceService = game:GetService("MarketplaceService") MarketplaceService.ProcessReceipt = function(receiptInfo) local players = game.Players:GetPlayers() for _, player in pairs(players) do -- player = v; _ = i when not needed. if player.UserId == receiptInfo.PlayerId and receiptInfo.ProductId == id then -- Make sure it's the product the player brought player.leaderstats:WaitForChild(stat).Value = player.leaderstats:WaitForChild(stat).Value + award end end return Enum.ProductPurchaseDecision.PurchaseGranted end
Any questions? Leave them in the comments! Thanks and I hope this will help. :)