My issue is that I do not know how to check if a player has actually bought a dev product or not.
local MarketplaceService = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local ProductID = 1186350446
local click = script.Parent.ClickDetector
click.MouseClick:Connect(function(player)
MarketplaceService:PromptProductPurchase(player, ProductID)
end)
local function gamepassPurchaseFinished()
print("PromptProductPurchaseFinished" .. ProductID .. " -Nuke_Product" )
--check if player has bought or not (how do i do it!!)
end
MarketplaceService.PromptProductPurchaseFinished:Connect(gamepassPurchaseFinished)
Right now, all the code works. I’ve tested it. I just need to know how to check if the player has bought the dev product or not where it says “–check if player has bought or not”
This script is a server script, under a part that’s under workspace.
If you need any additional information, be free to ask so! I will do my best to provide you any info you need.
local MarketplaceService = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local ProductID = 1186350446
local click = script.Parent.ClickDetector
click.MouseClick:Connect(function(player)
MarketplaceService:PromptProductPurchase(player, ProductID)
end)
local function gamepassPurchaseFinished(isPurchased)
print("PromptProductPurchaseFinished" .. ProductID .. " -Nuke_Product" )
if isPurchased then
click.Parent.Material = "DiamondPlate"
else
print("User opted out of purchase for product 1186350446")
end
end
MarketplaceService.PromptProductPurchaseFinished:Connect(gamepassPurchaseFinished)
For some reason, even though I pressed cancel, it still made the part Diamond Plate. It also did not print “User opted out of purchase for product 1186350446” in output. I do not know why. No errors showed up in output
It is still connecting to the function though, because whenever I do buy or cancel, it prints “PromptProductPurchaseFinished” … ProductID … " -Nuke_Product" so it is definitely an issue with the if statement (probably the isPurchased). I just do not know why isPurchased is not working. I made it a parameter within the function. Should I add another set of parentheses for the parameter, or maybe add another parameter to go with it?
local function gamepassPurchaseFinished(isPurchased, assetId)
print("PromptProductPurchaseFinished" .. assetId .. " -Nuke_Product" )
And it does work, printing the correct asset ID. So it isn’t to do with the parameters, and at least that narrows it down. I still am confused what the issue is however.
I tried implementing process receipt, to see if it would work.
--services
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
--player stuff
local player = game.Players.LocalPlayer
local Players = game:GetService("Players")
--Asset ID's
local NukeProductID = 1186350446
--buttons and prompt openers and stuff
local click = game.Workspace.NukeButton.ClickDetector
click.MouseClick:Connect(function(player)
MarketplaceService:PromptProductPurchase(player, NukeProductID)
end)
local productFunctions = {}
productFunctions[1186350446] = function(receipt, player)
if click.Parent then
click.Parent.Material = "DiamondPlate"
return true
end
end
-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)
-- Determine if the product was already granted by checking the data store
local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
local purchased = false
local success, errorMessage = pcall(function()
purchased = purchaseHistoryStore:GetAsync(playerProductKey)
end)
-- If purchase was recorded, the product was already granted
if success and purchased then
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif not success then
error("Data store error:" .. errorMessage)
end
-- Find the player who made the purchase in the server
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
-- The player probably left the game
-- If they come back, the callback will be called again
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- Look up handler function from 'productFunctions' table above
local handler = productFunctions[receiptInfo.ProductId]
-- Call the handler function and catch any errors
local success, result = pcall(handler, receiptInfo, player)
if not success or not result then
warn("Error occurred while processing a product purchase")
print("\nProductId:", receiptInfo.ProductId)
print("\nPlayer:", player)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- Record transaction in data store so it isn't granted again
local success, errorMessage = pcall(function()
purchaseHistoryStore:SetAsync(playerProductKey, true)
end)
if not success then
error("Cannot save purchase data: " .. errorMessage)
end
-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
return Enum.ProductPurchaseDecision.PurchaseGranted
end
-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt
No errors in output, and not doing anything, no prints, nothing. This is mostly roblox API script, I only added the player variables, click variables, Asset ID variable, and the following code:
click.MouseClick:Connect(function(player)
MarketplaceService:PromptProductPurchase(player, NukeProductID)
end)
local productFunctions = {}
productFunctions[1186350446] = function(receipt, player)
if click.Parent then
click.Parent.Material = "DiamondPlate"
return true
end
end
its because first parameter is UserId and you named it isPurchased
do what I said before
local function gamepassPurchaseFinished(UserId, Product, isPurchased)
if Product == ProductID and UserId == player.UserId and isPurchased then
-- code here
else
-- another one here
end
end
local MarketplaceService = game:GetService("MarketplaceService")
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, assetId, isGamepassPurchased)
if isGamepassPurchased == true and assetId == "There is your gamepassid" then
"If player has pass"
else
"If not"
end
end)