The UserId is a
Player property that contains a read-only integer that uniquely and consistently identifies every user account on Roblox. Unlike the
Instance/Name of a Player, which may change according the user’s present username, this value will never change for the same account.
This property is essential when saving/loading player data using
GlobalDataStores. Use a player’s UserId as the data store key so that each player has a unique key.
Data Store to Leaderboard
This code sample retrieves a player’s saved gold from a data store and puts the returned value onto the leaderboard. Note that this sample does not save players’ gold — it only loads it.
local Players = game:GetService("Players") local goldDataStore = game:GetService("DataStoreService"):GetDataStore("Gold") local STARTING_GOLD = 100 local function onPlayerAdded(player) local playerKey = "Player_" .. player.UserId local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" local gold = Instance.new("IntValue", leaderstats) gold.Name = "Gold" local myGold local success, err = pcall(function() myGold = goldDataStore:GetAsync(playerKey) end) if success then gold.Value = myGold else gold.Value = STARTING_GOLD end leaderstats.Parent = player end for _, player in pairs(Players:GetPlayers()) do onPlayerAdded(player) end Players.PlayerAdded:Connect(onPlayerAdded)
The below example would print the UserId of every user who entered a game.
game.Players.PlayerAdded:Connect(function(player) print(player.UserId) end)
local player = game:GetService("Players"):GetPlayerByUserId(1) if player then print("Player with userId 1 is in this server! His name is: " .. player.Name) else print("Player with userId 1 is not in this server!") end
Met the creator badge
The following code sample gives an example of a ‘met the creator’ badge system. This script will award a specified badge (BADGE_ID) to anyone who is in a server at the same time as the user associated with OWNER_ID.
local BadgeService = game:GetService("BadgeService") local Players = game:GetService("Players") -- change these local OWNER_ID = 212423 -- can use game.CreatorId for published places local BADGE_ID = 1 local ownerInGame = false local function playerAdded(newPlayer) if newPlayer.UserId == OWNER_ID then -- if new player is the owner, set ownerInGame to true and give everyone the badge ownerInGame = true for _, player in pairs(Players:GetPlayers()) do -- don't award the owner if player ~= newPlayer then BadgeService:AwardBadge(player.UserId, BADGE_ID) end end elseif ownerInGame then -- if the owner is in the game, award the badge BadgeService:AwardBadge(newPlayer.UserId, BADGE_ID) end end local function playerRemoving(oldPlayer) if oldPlayer.UserId == OWNER_ID then -- set ownerInGame to false ownerInGame = false end end -- listen for players joining and leaving Players.PlayerAdded:Connect(playerAdded) Players.PlayerRemoving:Connect(playerRemoving) -- fire playerAdded for existing players for _, player in pairs(Players:GetPlayers()) do playerAdded(player) end