GetPlayerByUserId returns "nil"

Hello! So I’m trying to fetch the player using their user id but the command “GetPlayerByUserId” returns nil.

This is the Server Side. (Where it returns nil)
image

And this is where it sends the UserId from the local script.

pcall(function() local UserId = game.Players:GetUserIdFromNameAsync(TextBox.Text)
      print(UserId)
      Event:FireServer(UserId)
    end)

and yes it prints the userid normally without any issue.
(Nothing is wrong in the local script)

If you know a solution, please let me know! Thank you! :slight_smile:

In the local script, you’re not sending the userid, you’re sending the player that you got with the userid with GetUserIdFromNameAsync. You’re basically sending a player variable.

Also, when printing the player’s name, don’t do print(Player), as you’re printing the instance and not the string/name. Use print(Player.Name).

No no, it prints the userid without a problem and i used the user id in a function and it worked well.
image

Maybe it prints “nil” because you are trying to print player instance? You should try to print player.Name or something like that

tried that, didn’t work out.
image

Try replacing the UserId in the local script with game.Players.LocalPlayer.UserId just for checking purposes and reply with the output. I think that might fix it, as the server’s picking up a player instance.

1 Like

that worked and did as the code told it to do.
image
but im trying to get the id from the player name in a textbox which prints the user id but returns to nil when i try to turn the userid into a player

Do player you are trying to get is in server? According to this article it will Search for players in the server

1 Like

Make 2 different variables, one for the userid, one for the player. This can both be used locally and serversided-ly.

local UserId = game.Players.LocalPlayer.UserId; -- Returns UserId
local Player = game:GetPlayerByUserId(UserId); -- Returns Player
1 Like

oh really? oof. How can i get their player without them being in the server?

Oh, that’s different. You can’t get a player object from a player that’s not in game, are you trying to get someone’s avatar instead?

2 Likes

OH wait i just need their name so i can do GetNameFromUserIdAsync

Thanks for helping i solved the issue. Who should i put as solution?

1 Like

Your choice, I don’t really need it. I will grant @ShkatulkaGames it.

1 Like

Alrighty Thanks guys!

2 Likes