I have a script that buys an item and does something when its bought but for some reason it just gives the purchase failed error.
local marketplace = game:GetService("MarketplaceService")
local productId = 955894489
local player = game.Players.LocalPlayer
-- Function to handle the purchase
local function onProductPurchaseFinished(userId, purchasedProductId, isPurchased)
if isPurchased and purchasedProductId == productId then
-- Find all players excluding the buyer
local otherPlayers = {}
for _, otherPlayer in pairs(game.Players:GetPlayers()) do
if otherPlayer ~= player then
table.insert(otherPlayers, otherPlayer)
end
end
-- Check if there are any other players available
if #otherPlayers > 0 then
-- Choose a random player from the list
local randomPlayer = otherPlayers[math.random(1, #otherPlayers)]
-- Create an explosion at the random player's character
if randomPlayer.Character then
local explosion = Instance.new("Explosion")
explosion.Position = randomPlayer.Character.HumanoidRootPart.Position
explosion.BlastRadius = 10
explosion.BlastPressure = 500000
explosion.Parent = randomPlayer.Character
end
end
end
end
-- Connect the purchase prompt to the button click
script.Parent.MouseButton1Click:Connect(function()
marketplace:PromptProductPurchase(player, productId)
end)
-- Listen for the product purchase completion
marketplace.PromptProductPurchaseFinished:Connect(onProductPurchaseFinished)