I am trying to make a GUI that sells dev products. In this case, i am trying to make it give you a certain amount of Credits (250) after purchasing a dev product. I can't seem to find the problem here... Can anyone help?
-- setup local variables local MarketplaceService = Game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory") local productId = 20768662 -- define function that will be called when purchase finished MarketplaceService.ProcessReceipt = function(receiptInfo) -- find the player based on the PlayerId in receiptInfo for i, player in ipairs(game.Players:GetChildren()) do if player.userId == receiptInfo.PlayerId then -- check which product was purchased if receiptInfo.ProductId == productId then --handle purchase. In this case we are highering the player's Credits value plr.leaderstats.Credits.Value = plr.leaderstats.Credits.Value + 250 -- more feedback for the player. game.Workspace.DisplayScreen.SurfaceGui.TextBox.Text = player.Name .. " has purchased more credits." end end end end
You didn't define player.
Whenever you see that kind of error, remember that it means you have to define it. It doesn't know what plr is
Here put this code in a script then put the script in StarterPack:
local MarketplaceService = Game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory") CASHID = 00000000 -- Put the Developer Product ID here and erase the 0's MarketplaceService.ProcessReceipt = function(receiptInfo) local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_product_" .. receiptInfo.ProductId local numberBought = ds:IncrementAsync(playerProductKey, 1) for i,v in pairs (game.Players:GetChildren()) do if v.userId == receiptInfo.PlayerId then if receiptInfo.ProductId == CASHID then lds = v:FindFirstChild("leaderstats") if lds ~= nil then cs = lds:FindFirstChild("Credits") --Change to your leaderstat if cs ~= nil then cs.Value = cs.Value + 250 --Change to the amount you want this to add end end end end end return Enum.ProductPurchaseDecision.PurchaseGranted end
Don't forget to say this answered you and give it a thumbs up because I am working for a good reputation!
On line 16 change plr to player. Plr is nil to the script since it was never defined.
And add to line 19 or else the player will get credits on entry. return Enum.ProductPurchaseDecision.PurchaseGranted
Please refrain from reposting this question.
Any errors, just comment on this answer.
-- setup local variables local MarketplaceService = Game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory") local productId = 20768662 -- define function that will be called when purchase finished MarketplaceService.ProcessReceipt = function(receiptInfo) -- find the player based on the PlayerId in receiptInfo for i, player in ipairs(game.Players:GetChildren()) do if player.userId == receiptInfo.PlayerId then -- check which product was purchased if receiptInfo.ProductId == productId then --handle purchase. In this case we are highering the player's Credits value player.leaderstats.Credits.Value = plr.leaderstats.Credits.Value + 250 -- more feedback for the player. game.Workspace.DisplayScreen.SurfaceGui.TextBox.Text = player.Name .. " has purchased more credits." return Enum.ProductPurchaseDecision.PurchaseGranted --this is line 19. end end end end