Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i get the players name form a id?

Asked by 2 years ago

Hello so i'm creating a script and i'm using tables and i have to use UserId but now i don't know how to get the players name from his UserId can somebody help me?

Here's the code:

local playeringame = {}
local Status = game.ReplicatedStorage:WaitForChild("Status")

Status.Value = "Waiting For Players..." 

game.Players.PlayerAdded:Connect(function(player)
    repeat
        wait()
    until #game:GetService("Players"):GetPlayers() >= 1

    wait(2)

    for i = 4, 1, -1 do
        Status.Value = "Staring Game in "..i
        wait(1)
    end

    Status.Value = "Loading Map..."

    local ClonedMap = game.ReplicatedStorage.Maps.Map01:Clone()
    ClonedMap.Parent = workspace
    ClonedMap.Name = "Map"
    wait(2) 

    game.ReplicatedStorage.NumbersPlayersInGame.Value = #game:GetService("Players"):GetPlayers()
    table.insert(playeringame, player.UserId)

    player.Character.HumanoidRootPart.CFrame = workspace:WaitForChild("Map").Spawns.Spawn.CFrame

    player.Character.Humanoid.Died:Connect(function()
        game.ReplicatedStorage.NumbersPlayersInGame.Value -= 1
        table.remove(playeringame, player.UserId)
    end)

    repeat
        wait()
    until game.ReplicatedStorage.NumbersPlayersInGame.Value == 1

    if game.ReplicatedStorage.NumbersPlayersInGame.Value == 1   then
        Status.Value = "Game Ended"
        game.ReplicatedStorage.NumbersPlayersInGame.Value = 0
        game.ReplicatedStorage.LastPlayer.Value = playeringame[1]
        print(playeringame[1])
    end

end)

1 answer

Log in to vote
0
Answered by 2 years ago

you can just use the method

game.Players:GetPlayerByUserId(userid).Name

the .Name part just gets the player object name so if you want to get the displayname just change it to .DisplayName if no player in the game has the userid (for example they left or something) then it will return nil.

or alternatively there also exists this method

game.Players:GetNameFromUserIdAsync(userid)

though i wouldn't use it because it gets it from the roblox website instead so you'd have to wrap the function in a pcall

i don't see why you wouldnt just insert the player into the table rather than the userid

also you might want to consider not having it all connected to the playeradded event.

0
Thanks Man! tyfftjftgdchgf 17 — 2y
Ad

Answer this question