How can I implement a player's username on NPC in accordance to current userdefinition

I have a script located in the humanoid of an npc in my game that essentially gives all values of the humanoid description to that very npc, however; I need to be able to know how to somehow be able to have the npc also be given the appropiate username in sync with the current player id called (player id appearance is randomized). Here is the current code that I have:

-- Getting a users description

local playerid = {"29167537", "604686747", "1068512944", "440764293", "121874607", "858721856", "349105627", "1431587966"}

local randomid = playerid[math.random(1, #playerid)]

-- Getting a users description

local humanoidDescriptionForUser = game.Players:GetHumanoidDescriptionFromUserId(randomid)

-- Applying the description to the NPC

script.Parent:ApplyDescription(humanoidDescriptionForUser)

There is a method in the Players service: Players:GetNameFromUserIdAsync(userId)
It is recommended you wrap this function in a protected call if you use it to catch any errors (userid does not exist, etc)

If the players are expected to be in the game, you can also run code similar to this:

local player = Players:GetPlayerByUserId(userId)
if player then
return player.Name
end

You can then name the model of the NPC the player’s username or you can setup a custom GUI if your NPC is not a default humanoid.

1 Like

It doesn’t seem to be working; I must be misunderstanding:

-- Getting a users description
local playerid = {"29167537", "604686747", "1068512944", "440764293", "121874607", "858721856", "349105627", "1431587966", "356356384"}
local randomid = playerid[math.random(1, #playerid)]

-- Getting a users description

local humanoidDescriptionForUser = game.Players:GetHumanoidDescriptionFromUserId(randomid)

game.Players:GetNameFromUserIdAsync(randomid)
if player then
return player.Name
end
-- Applying the description to the NPC

script.Parent:ApplyDescription(humanoidDescriptionForUser)

Any ideas?

That was just pseudocode, it won’t do anything unless you incorporate it with your existing code.

assuming script.parent is the Humanoid, and assuming that i’d want to name the model after the player, i would change return player.Name to script.Parent.Parent.Name = player.Name

And that will only work if the player is in the game when you’re trying to get the name.

Then what form of code for this do I specifically need in order to grab the username? The code lists the circumstance that the randomid is the current chosen player id for the humanoid description so that it can disguise the npc as the avatar that represents that player id but I need to know how to have the npc named in accordance with the current given id (aka the humanoid description of the avatar that is set by the profile id), even if the actual real player is not in the game.

edit: as a sidenote, yes; you had the correct parenting if that matters to you in any way

You can use Players:GetNameFromUserIdAsync() to get a player name without them actually being in the game.

You just need to make sure to wrap this in a protected call because it sends a request to the API.

You can find more documentation on that here: