local MarketplaceService = game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("countdowncreditservice") local productID = 20518668 local buy = script.Parent buy.MouseButton1Click:connect(function() game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productID) end) MarketplaceService.ProcessReceipt = function(receiptInfo) for i, player in ipairs(game.Players:GetChildren()) do if player.userId == receiptInfo.PlayerId then if receiptInfo.ProductId == productID then player.statistics.credits.Value = player.statistics.credits.Value + 1 local key = "plyr-"..player.userId local savedvalue = {player.statistics.credits.Value} ds:SetAsync(key, savedvalue) return Enum.ProductPurchaseDecision.PurchaseGranted end end end end
This is in a normal script within the player's StarterGui in a GUI.
Here's my error: MarketplaceService.PromptProductPurchase() player should be of type Player but is of type nil
This is pertaining to line 8, game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productID)
.
Does anybody know what the problem is?
Only LocalPlayer can be used with LocalScripts. However, MarketplaceService must be used with normal Scripts. Therefore you cannot use LocalPlayer; Instead you must parent up to the player itself.
Let's assume that the script is 4 down from Player. That means script.Parent.Parent.Parent.Parent would equal the player. I don't know the hierarchy of your GUI, so you must count the parents until you get to the player. Check out Line 8.
local MarketplaceService = game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("countdowncreditservice") local productID = 20518668 local buy = script.Parent buy.MouseButton1Click:connect(function() game:GetService("MarketplaceService"):PromptProductPurchase(script.Parent.Parent.Parent.Parent, productID) --In explorer, let's say that your GUI looks like this |Player > PlayerGui > ScreenGui > BuyButton > Script| That means you need 4 Parents, since you go 4 up from the script, which equals the player. If you have a frame or something which holds the Buy Button, add a Parent. end) MarketplaceService.ProcessReceipt = function(receiptInfo) for i, player in ipairs(game.Players:GetChildren()) do if player.userId == receiptInfo.PlayerId then if receiptInfo.ProductId == productID then player.statistics.credits.Value = player.statistics.credits.Value + 1 local key = "plyr-"..player.userId local savedvalue = {player.statistics.credits.Value} ds:SetAsync(key, savedvalue) return Enum.ProductPurchaseDecision.PurchaseGranted end end end end
You can read more on hierarchy here.
If I helped you, be sure to click the Accept Answer button below my character! :D