Getting a user id

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.

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

Put this in a local script btw

local player = game.Players.LocalPlayer
local id = -- put the admin id here
	
if player.UserId == id then
	-- put your script here
end
1 Like

I like your method but id like to have it so I can send player value name or player id
how can I implement that in your system

1 Like

What do you mean send the user id? Like through a RemoteEvent?

1 Like

i need it for the server also using local script to take admin action is a weak security as exploit chan change there id

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.

1 Like

I would make a table of each admin’s UserId, and once a player joins test of their playerID matches any of the UserID in the table

il explain a bit more…

I want to make a function that will

if I send a name return their id if the name is a valid name
if I send an id return the id if the id is a valid player

you gave me the part for getting their name id like if you can tell me how to add the part where I can verify if an id I send is a valid user id

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 can try use this

3 Likes

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

2 Likes

Oh, I don’t know how you could execute that since I’m not that advanced.

1 Like

You want GetPlayerByUserId()

1 Like
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.

can this tread be close?

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

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.