How to check how much robux a player has spent on your game?

Hey there. I’m just wondering if there’s a way to see how much robux a player has spent on your game. Like when you purchase a donation dev product, it displays how much robux the player has spent on the donations. I’m assuming this involves datastore too.

This is my code so far. It’s basicly just the dono buttons script

--ForeverBloxian
local marketplaceService = game:GetService('MarketplaceService')

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local buttonFrame = script.Parent.Body.DonateButtonsFrame

--buttons
local donoTier1Button = buttonFrame.DonationTier1Button
local donoTier2Button = buttonFrame.DonationTier2Button
local donoTier3Button = buttonFrame.DonationTier3Button
local donoTier4Button = buttonFrame.DonationTier4Button


--donoID
local donoTier1ID = 1697603831
local donoTier2ID = 1697603834
local donoTier3ID = 1697603830
local donoTier4ID = 1697603828

script.Parent.TopBar.CloseButton.MouseButton1Click:Connect(function()
	script.Parent.Visible=false
	script.Parent.Active=false
end)

donoTier1Button.MouseButton1Click:Connect(function()
	marketplaceService:PromptProductPurchase(player, donoTier1ID)
end)
donoTier2Button.MouseButton1Click:Connect(function()
	marketplaceService:PromptProductPurchase(player, donoTier2ID)
end)
donoTier3Button.MouseButton1Click:Connect(function()
	marketplaceService:PromptProductPurchase(player, donoTier3ID)
end)
donoTier4Button.MouseButton1Click:Connect(function()
	marketplaceService:PromptProductPurchase(player, donoTier4ID)
end)
2 Likes

You could use a datastore like so:

local DataStoreService = game:GetService("DataStoreService")
local Datastore = DataStoreService:GetDataStore("DATASTORE_NAME")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Success, AmountPurchased = pcall(function()
		Datastore:GetAsync(Player.UserId.."KEY_NAME")
	end)
	if Success and AmountPurchased then
		-- Do something
	elseif not Success
		warn(AmountPurchased)
	end
end)

if something purchased then
	local success, errormessage = pcall(function()
		return Datastore:SetAsync(Player.UserId.."KEY_NAME", Datastore:GetAsync(Player.UserId.."KEY_NAME") + however much it costed)
	end
	if success then
		-- do something
	else
		warn(errormessage)
	end
end
5 Likes

Every time a purchase is made check how much it costs and add it to a table which is saved in a datastore.

3 Likes

Roblox has been able to save tables to the datastore for a while now

6 Likes

This is probably what you are looking for : https://devforum.roblox.com/t/i-need-help-with-marketplaceserviceprocessreceipt/530751/7?u=milkyfiend

Below is a little example approach that I’ve put together for you to help point you in the right direction with this.

You will obviously be handling this entirely on the server side.

This would handle Product and GamePass purchases made on your game using the PromptProductPurchaseFinished and PromptGamePassPurchaseFinished MarketplaceService events.

Once a successful purchase is made, simply getPrice() with the correct Product InfoType and update an existing datastore key, adding to the player’s individual total spend.

-- ServerScript

local Players = game:GetService("Players")
local MPS = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("Purchases_V1")

local function getPrice(assetId:number, infoType:Enum.InfoType?)
	local productInfo = nil
	local succ, err = pcall(function()
		productInfo = MPS:GetProductInfo(assetId, infoType)
	end)
	
	if succ and productInfo and productInfo.IsForSale then
		return productInfo.PriceInRobux
	else
		return nil
	end
end

MPS.PromptProductPurchaseFinished:Connect(function(userId, productId, purchased)
	local productPrice = getPrice(productId, Enum.InfoType.Product)
	
	if purchased and productPrice then
		-- Modify the player's DataStore key and add the returned productPrice to their total spend
	end
end)

MPS.PromptGamePassPurchaseFinished:Connect(function(plr, gamepassId, purchased)
	local productPrice = getPrice(gamepassId, Enum.InfoType.GamePass)
	
	if purchased and productPrice then
		-- Modify the player's DataStore key and add the returned productPrice to their total spend
	end
end)
1 Like