How to find player's DisplayName with a textbox by putting their DisplayName?

Well, I already have the script that searches for the player with his name but not with his DisplayName and I want the same script to search for the player with the player.DisplayName or with player.Name

local TextBox = script.Parent:FindFirstChild("TextBox")
local Players = game:GetService("Players")
local RealName = script.Parent:FindFirstChild("RealName")

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	RealName.Text = "Search the player..."
	wait(0.8)
	local PlayerTable = {}
	for _, Plr in pairs(game.Players:GetPlayers()) do 
		table.insert(PlayerTable, tostring(Plr))
	end

	local CurrentText = TextBox.Text
	local PlayerDetected = table.find(PlayerTable, CurrentText) --This returns either the Player String or nil

	if PlayerDetected then
		RealName.Text = "@"..PlayerTable[PlayerDetected]
	elseif CurrentText == "" then
		RealName.Text = "@Player Name"
	else --If both of these aren't valid, we tell the player that this said player doesn't exist
		RealName.Text = "This player is not in the server"
	end
end)
2 Likes

We can easily do this using: Player | Roblox Creator Documentation, the only problem is if another player has the same displayname!

function get_plr_from_displayname(displayName)
	for _, player in ipairs(game:GetService('Players'):GetPlayers()) do
		if string.lower(player.DisplayName):match(string.lower(displayName)) then
			return player
		end
	end
	
	return 'Player not in the server!'
end


wait(5)

print(get_plr_from_displayname("DevBeat"))
1 Like

That’s because when you store a string, you’re only storing the string of the player’s name, not their display name.

I recommend you rework your methodology in a way similar to this:

local players = game:GetService('Players')
textBox:GetPropertyChangedSignal("Text"):Connect(function()
    realName.Text = 'Searching for player...'
    wait(.8)
    local foundPlayer
    for i,v in pairs(players:GetPlayers()) do
        if v.Name == textBox.Text then
            realName.Text = '@'..v.Name
        elseif v.DisplayName == textBox.Text then
            realName.Text = '@Player Name'
        end
    end
end)

In fact, the original script was similar.

local TextBox = script.Parent:FindFirstChild("TextBox")
local Players = game:GetService("Players")
local RealName = script.Parent:FindFirstChild("RealName")
local ImagePlayer = script.Parent:FindFirstChild("PlayerImage")


local Main = script.Parent.Parent.Parent.Parent

local Player1 = Main:FindFirstChild("Player1")


TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	RealName.Text = "Searching the player..."
	wait(0.8)
	for _, players in pairs (Players:GetChildren()) do
		if (TextBox.Text == players.Name) or (TextBox.Text == players.DisplayName) then
			local UserId = players.UserId
			local thumbType = Enum.ThumbnailType.HeadShot
			local thumbSize = Enum.ThumbnailSize.Size420x420
			local content, isReady = Players:GetUserThumbnailAsync(UserId, thumbType, thumbSize)

			ImagePlayer.Image = content
			Player1.Value = players.Name
			RealName.Text = "@".. players.Name
		elseif TextBox.Text == "" then
			RealName.Text = "@Player Name"
			Player1.Value ="N/A"
			ImagePlayer.Image = ""
		elseif (TextBox.Text and players.Name) == (TextBox.Text and players.DisplayName) then
			RealName.Text = "No exist the player."
			Player1.Value ="N/A"
			ImagePlayer.Image = ""
		end
	end
end)

But with the third elseif elseif, when I looked for other players and scored them well, I would put what comes out in the third elseif