Type Index Pages
- BillboardGui
- CanvasGroup
- Frame
- Hint
- ImageButton
- ImageLabel
- PlayerGui
- ScreenGui
- ScrollingFrame
- StarterGui
- SurfaceGui
- TextBox
- TextButton
- TextLabel
- UIAspectRatioConstraint
- UIGradient
- UIGridLayout
- UIListLayout
- UIPadding
- UIPageLayout
- UIScale
- UISizeConstraint
- UITableLayout
- UITextSizeConstraint
- VideoFrame
- ViewportFrame
- ChangeHistoryService
- CoreGui
- DataModelSession
- DockWidgetPluginGui
- MultipleDocumentInterfaceInstance
- Plugin
- PluginAction
- PluginGui
- PluginGuiService
- PluginManager
- PluginMenu
- PluginMouse
- PluginToolbar
- PluginToolbarButton
- QWidgetPluginGui
- Selection
- StandalonePluginScripts
- StatsItem
- StudioService
- StudioTheme
No results found!
-
Instance
- GameSettings
- DebugSettings
- BodyMover
- WeldConstraint
- HttpRbxApiService
- NotificationService
- Translator
- Lighting
- Beam
- GuiService
- UserInputService
- Studio
- Plugin
- HttpService
- Mouse
- BindableEvent
- RunService
- Pages
- Humanoid
- TestService
- PathfindingService
- Chat
- NetworkPeer
- Feature
- CharacterAppearance
- Constraint
- NetworkReplicator
- JointInstance
- Light
- BasePlayerGui
- AnalyticsService
- NetworkMarker
- BinaryStringValue
- FlyweightService
- Geometry
- LoginService
- InstancePacketCache
- ThirdPartyUserService
- TouchInputService
- RuntimeScriptService
- GuidRegistryService
- PartOperationAsset
- DialogChoice
- PhysicsService
- AdService
- TextService
- MarketplaceService
- TeleportService
- Accoutrement
- GamePassService
- AssetService
- InsertService
- PointsService
- ChangeHistoryService
- ServerScriptService
- JointsService
- LogService
- InputObject
- Toolbar
- LuaSettings
- RenderSettings
- AnimationTrack
- PhysicsSettings
- NetworkSettings
- CFrameValue
- Animation
- Color3Value
- BoolValue
- BrickColorValue
- Vector3Value
- AnimationController
- BindableFunction
- Button
- Trail
- LocalizationTable
- LocalizationService
- DebuggerBreakpoint
- DebuggerWatch
- ScriptDebugger
- Animator
- Attachment
- RemoteFunction
- RemoteEvent
- PluginManager
- Camera
- Stats
- Sky
- StarterPlayer
- Dragger
- TerrainRegion
- Path
- TextFilterResult
- Dialog
- StatsItem
- GoogleAnalyticsConfiguration
- ScriptContext
- ControllerService
- CacheableContentProvider
- ReflectionMetadataClasses
- ReflectionMetadataEnums
- DebuggerManager
- GuiBase
- UIBase
- LuaSourceContainer
- GuiItem
- DataModelMesh
- ServiceProvider
- ReflectionMetadataItem
- PostEffect
- PhysicsPacketCache
- TouchTransmitter
- RobloxReplicatedStorage
- Visit
- LuaWebService
- ScriptService
- FlagStandService
- VirtualUser
- SpawnerService
- TimerService
- CookiesService
- Team
- GroupService
- StarterGear
- Message
- PlayerScripts
- Configuration
- ContentProvider
- CollectionService
- Debris
- ReplicatedFirst
- ServerStorage
- ReplicatedStorage
- Folder
- TweenService
- Players
- ContextActionService
- StarterPlayerScripts
- SoundService
- KeyframeSequenceProvider
- VRService
- PluginGuiService
- Player
- Teams
- Pose
- Keyframe
- KeyframeSequence
- IntConstrainedValue
- DoubleConstrainedValue
- ForceField
- RayValue
- Fire
- Smoke
- Sparkles
- ParticleEmitter
- IntValue
- StringValue
- NumberValue
- Explosion
- ObjectValue
- SoundGroup
- UserGameSettings
- ClickDetector
- Sound
- Selection
- BadgeService
- TaskScheduler
- GlobalDataStore
- DataStoreService
- CustomEvent
- CustomEventReceiver
- VirtualInputManager
- FunctionalTest
- TweenBase
- SoundEffect
- ReflectionMetadataEvents
- ClusterPacketCache
- PVInstance
- FaceInstance
- Controller
- ReflectionMetadataCallbacks
- ReflectionMetadataFunctions
- ReflectionMetadataYieldFunctions
- ReflectionMetadataProperties
- ReflectionMetadata
- AdvancedDragger
- HapticService
- FriendService
- GamepadService
No Result Found !!!
GetPlayerByUserId
For thread safety, this property is not safe to read in an unsynchronized thread.
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 |
Returns
Return Type | Summary |
---|---|
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 end end -- 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 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 success, isPurchaseRecorded = pcall(function() return purchaseHistoryStore:UpdateAsync(playerProductKey, function(alreadyPurchased) if alreadyPurchased then return true 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 nil end local handler = productFunctions[receiptInfo.ProductId] local success, result = pcall(handler, receiptInfo, player) -- If granting the product failed, do NOT record the purchase in datastores. if not success or not result then error("Failed to process a product purchase for ProductId:", receiptInfo.ProductId, " Player:", player) return nil end -- Record the transcation in purchaseHistoryStore. return true end) end) if not success then error("Failed to process receipt due to data store error.") return Enum.ProductPurchaseDecision.NotProcessedYet elseif isPurchaseRecorded == nil then -- Didn't update the value in data store. return Enum.ProductPurchaseDecision.NotProcessedYet else -- IMPORTANT: Tell Roblox that the game successfully handled the purchase return Enum.ProductPurchaseDecision.PurchaseGranted end end -- Set the callback; this can only be done once by one script on the server! MarketplaceService.ProcessReceipt = processReceipt
Players:GetPlayerByUserId
local player = game:GetService("Players"):GetPlayerByUserId(1) if player then print("Player with userId 1 is in this server! Their name is: " .. player.Name) else print("Player with userId 1 is not in this server!") end
How this site use cookies
This Platform uses cookies to offer you a better experience, to personalize content, to provide social media features and to analyse the traffic on our site. For further information, including information on how to prevent or manage the use of cookies on this Platform, please refer to our Privacy and Cookie Policy.