--LocalScript inside button local buyButton = script.Parent local productId = 20518668 buyButton.MouseButton1Click:connect(function() game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productId) end) local MarketplaceService = Game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory") local productId = 20518668 MarketplaceService.ProcessReceipt = function(receiptInfo) for i, player in ipairs(game.Players:GetChildren()) do if player.userId == receiptInfo.PlayerId then if receiptInfo.ProductId == productId then local Item = game.ReplicatedStorage.Swords.Sword Item:Clone().Parent = player.Backpack Item:Clone().Parent = player.StarterGear end end end local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId ds:IncrementAsync(playerProductKey, 1) return Enum.ProductPurchaseDecision.PurchaseGranted end
I want this set so once a player buys a dev product they get a sword in their inv. Please help.
I don't think that you can get services from a local script, so you have to use both a normal script and local script.
Here is what the scripts might look like:
--Normal script in ServerScriptStorage local MarketplaceService = Game:GetService("MarketplaceService") local Func = Instance.new("RemoteFunction" , game.ReplicatedStorage) Func.Name = "PromptPlayer" Func.OnServerInvoke = function(PlayerToPrompt , productID) MarketplaceService:PromptProductPurchase(PlayerToPrompt , productID) end
Here is the local script:
local buyButton = script.Parent local ProductID = 20518668 buyButton.MouseButton1Click:connect(function() game.ReplicatedStorage.PromptPlayer:InvokeServer(ProductID) end)
Now you have to process the "receipt" to award the sword. Create a separate script and put it in ServerScriptStorage.
local MarketplaceService = game:GetService("MarketplaceService") local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory") local sword = 20518668 MarketplaceService.ProcessReceipt = function(receiptInfo) local playerProductKey = receiptInfo.PlayerId .. ":" .. receiptInfo.PurchaseId for i, player in ipairs(game.Players:GetChildren()) do if player.userId == receiptInfo.PlayerId then if receiptInfo.ProductId == sword then local Item = game.ReplicatedStorage.Swords.Sword Item:Clone().Parent = player.Backpack Item:Clone().Parent = player.StarterGear player:SaveBoolean("HasSword" , true) end end end ds:SetAsync(playerProductKey , true) return Enum.ProductPurchaseDecision.PurchaseGranted end
If you want the player to always have the sword, add this normal script to ServerScriptStorage
local Players = game.Players function onPlayerAdded(Player) if Player:WaitForDataReady() then if Player:LoadBoolean("HasSword") then local Item = game.ReplicatedStorage.Swords.Sword Item:Clone().Parent = Player.Backpack Item:Clone().Parent = Player.StarterGear end end end Players.PlayerAdded:connect(onPlayerAdded)
If this doesn't work, please tell me.
You have a typo on line 13, It says "ipairs" and it should say pairs.