so I’m making an admin system and id like to be able to get player id even if they are not in-game.
local function GetUserId(Value)
if not tonumber(Value) and Value:IsA("Player") then
return Value.UserId
elseif tonumber(Value) and Players:GetPlayerByUserId(Value) then
return Value
elseif tonumber(Value) and Players:GetNameFromUserIdAsync(Value) then
return Value
elseif not tonumber(Value) and Players:GetUserIdFromNameAsync(Value) then
return Players:GetUserIdFromNameAsync(Value)
else
return false
end
end
this is what I have as of right now id like to know if there is a more effective way to do this.
local function GetUserId(name: string)
return game:GetService("Players"):GetUserIdFromNameAsync(name)
end
If you want it prints ‘true’ or ‘false Players:GetUserIdFromNameAsync() failed: Unknown user’ instead of printing the UserId if exists and error if it doesn’t exist. then you’d use pcall.
local function GetUserId(name: string)
return pcall(function()
game:GetService("Players"):GetUserIdFromNameAsync(name)
end)
end
well the function will be call by my automatic modetation system and also via remote
the reson i want to send id is so i can verefy the id is to make sure its a valid player i can take action on even if there not in game.
The main topic is getting an user id so i guess you’d need to mark me as solution because it solves your problem *
For your thing, you can use it in a Server Script.
And taking action even if they aren’t in game i understand but you would need to make a DataStore system to store the user’s id so they are banned on all servers and not only on your current.
I would firstly make the table of the userID and use the player added function
game.Players.PlayerAdded:Connect(function(player)
Then test if the player’s UserID matches any of the ones in the table for example
Local Admins = {
—put your UserID here
}
game.Players.PlayerAdded:connect(function(player)
for _,admin in pairs(Admins) do
if player.UserID == admin then
—put your code here
end
end
end)
you dont understand what im trying to do i dont want to find admin in a table
i want to make a functin in witch im send a player value or a user name or a user id
i want the function to return me the asosiated user id if the player exist on roblox
local targetName = 'Username'
local targetId = game.Players:GetUserIdFromNameAsync(targetName)
local player
if not targetId then
return -- targetId is not valid; username provided does not exist
end
for i,v in pairs(game.Players:GetChildren()) do
if v.UserId == targetId then
player = v
end
end
if player then
-- Player is in-game, use player variable
else
-- Player is not in-game, use targetId and targetName variables
end
Pretty simple, if you want to get an offline player I suggest doing this.
Use this, so basically this script gets a players username from a textbox & even IF they are offline it would get it, goes with their userId too.
local Players = game:GetService("Players")
local function get_player_from_partial(str)
str = str:lower()
for _, player in ipairs(Players:GetPlayers()) do
if player.Name:lower():sub(1, #str) == str then
return player
end
end
return nil
end
local cache = {}
function getUserIdFromUsername(name)
if cache[name] then return cache[name] end
local player = Players:FindFirstChild(name)
if player then
cache[name] = player.UserId
return player.UserId
end
local id
pcall(function ()
id = Players:GetUserIdFromNameAsync(name)
end)
cache[name] = id
return id
end
local actualplr
local actualplrid
script.TextBox.Changed:Conncet(function()
local plr = get_player_from_partial(script.Textbox.Text)
if plr then
if script.TextBox.Text == "" then
actualplr = nil
actualplrid = nil
else
actualplr = plr.Name
actualplrid = plr.UserId
end
else
local actualplr = script.TextBox.Text
local userIdofOfflinePlr = getUserIdFromUsername(actualplr)
if userIdofOfflinePlr ~= nil then
actualplrId = userIdofOfflinePlr
end
end)
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Conncet(function(targetName,targetid)
targetName = actualplr
targetid = actualplrId
end)
Oh and by the way, this is on the client and you get the actuaplr & actualplrid from a textbox, that’s the only way i can think of other than @MyNameIsChaosity’s way.
local function GetUserId(Value)
if not tonumber(Value) and Players:FindFirstChild(tostring(Value)) then
return Players:FindFirstChild(tostring(Value)).UserId
else
local Id = false
local SuccesId, ReturnName = pcall(function()
Players:GetNameFromUserIdAsync(Value)
Id = Value
end)
local SuccesName, ReturnId = pcall(function()
Id = Players:GetUserIdFromNameAsync(Value)
end)
return Id
end
end
this is the solution I came up whit
it supports both name and user id and will verify them