PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

GetPlayerByUserId

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

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.

Parameters

Name Type Default Description

The Player/UserId of the player being specified

Returns

Return Type Summary

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


Code Samples


Players:GetPlayerByUserId


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.