How do I get player from Player Name?

  • How do i get Player from PlayerName?
local playername = "NEW_GODPOTATO1110"
local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent:FireClient(playername) --I cant do this

So I want to know that how to get player from playername.

2 Likes
local Player = Players:FindFirstChild(PlayerName)
4 Likes

The person above said something that will work, unless the player is not in the server yet. I reccomend doing something along these lines:

local playername = "name here"
local remoteEvent = game.ReplicatedStorage.RemoteEvent

wait(1)

if game:GetService("Players"):FindFirstChild(playername) then
     local player = game:GetService("Players"):FindFirstChild(playername)
     remoteEvent:FireClient(player)
else
     print("Player ".. playername.." is not in the server")
end
4 Likes

You can avoid repeating yourself by assigning whatever FindFirstChild returns to a variable.

local game = game
local players = game:GetService("Players")

local player = players:FindFirstChild(playerName)
if player then
	--Do code.
else
	--Do other code.
end
2 Likes

Thank you for helping me!!
It works!!
Thank you so muchhh :smiley:

1 Like