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)
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.