We use cookies on this site to enhance your user experience
Collapse Sidebar


This function searches each Player|player in Players for one whose Player/UserId matches the given UserId. If such a player does not exist, it simply returns nil. It is equivalent to the following function:

local Players = game:GetService("Players")
local function getPlayerByUserId(userId)
	for _, player in pairs(Players:GetPlayers()) do
		if player.UserId == userId then
			return player

This method is useful in finding the purchaser of a developer product using MarketplaceService/ProcessReceipt, which provides a table that includes the purchaser’s UserId and not a reference to the Player object itself. Most games will require a reference to the player in order to grant products.


Name Type Default Description

The Player/UserId of the player being specified


Return Type Summary

The player with the specified Player/UserId, or nil if that player is not in the game

Code Samples

ProcessReceipt Callback

This code sample illustrates a MarketplaceService/ProcessReceipt|ProcessReceipt callback function for a game to handle purchasing of two Articles/Developer Products In Game Purchases|developer products (more can be added as needed). It properly checks for and records purchases using a GlobalDataStore called “PurchaseHistory.”

Most importantly, it properly returns Enum.ProductPurchaseDecision.PurchaseGranted when the transaction is successfully completed or if it’s detected that the purchase has already been granted using the “PurchaseHistory” data store.

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

-- Data store for tracking purchases that were successfully processed
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")

-- Table setup containing product IDs and functions for handling purchases
local productFunctions = {}
-- ProductId 123123 for a full heal
productFunctions[123123] = function(receipt, player)
	-- Logic/code for player buying a full heal (may vary)
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		-- Heal the player to full health
		player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
		-- Indicate a successful purchase
		return true
-- ProductId 456456 for 100 gold
productFunctions[456456] = function(receipt, player)
	-- Logic/code for player buying 100 gold (may vary)
	local stats = player:FindFirstChild("leaderstats")
	local gold = stats and stats:FindFirstChild("Gold")
	if gold then
		gold.Value = gold.Value + 100
		-- Indicate a successful purchase
		return true

-- 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)
	-- 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)

	-- 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
	-- 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

	-- Record transaction in data store so it isn't granted again
	local success, errorMessage = pcall(function()
		purchaseHistoryStore:SetAsync(playerProductKey, true)
	if not success then
		error("Cannot save purchase data: " .. errorMessage)

	-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
	return Enum.ProductPurchaseDecision.PurchaseGranted

-- Set the callback; this can only be done once by one script on the server! 
MarketplaceService.ProcessReceipt = processReceipt


local player = game:GetService("Players"):GetPlayerByUserId(1)
if player then
    print("Player with userId 1 is in this server! His name is: " .. player.Name)
    print("Player with userId 1 is not in this server!")