How to find the player with a textbox in game.Players?

Well, as you can see, the script can already track the selected player with the textbox, but I want that if the player does not exist, something like “No player exists” appears in the text and what I put does not work for me. Just find the player and that’s it, but if it doesn’t exist, it doesn’t do the function I want.

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)
	for _, players in pairs (Players:GetChildren()) do
		if TextBox.Text == players.Name then
			RealName.Text = "@".. players.Name
		elseif TextBox.Text == "" then
			RealName.Text = "@Player Name"
		elseif TextBox.Text == not players.Name then
			RealName.Text = "Player not found."
		end
	end
end)
1 Like

Try this?

local Players = game:GetService("Players")

local TextBox = script.Parent:WaitForChild("TextBox")
local RealName = script.Parent:WaitForChild("RealName")

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	RealName.Text = "Search the player..."
	wait(0.8)

	for _, players in pairs(Players:GetChildren()) do
		if TextBox.Text == players.Name then
			RealName.Text = "@".. players.Name

		elseif TextBox.Text == "" then
			RealName.Text = "@Player Name"
		else
			RealName.Text = "Player not found."
		end
	end
end)
2 Likes

Hm, judging by what you want it looks like you’re wanting to detect if the Player you’re attempting to search for is currently in the game? What you could possibly do is reference a table of Players and use table.find() to check if that said “Player” is in the game to reference them, and if not it’ll send back “No Player Exists in this server”

Also do call WaitForChild(), it’s better than using FindFirstChild() as that could potentially error

local TextBox = script.Parent:WaitForChild("TextBox")
local Players = game:GetService("Players")
local RealName = script.Parent:WaitForChild("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 = "@"..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)
1 Like

It works but doesn’t put the player’s name in the text

2 Likes

Try this

local TextBox = script.Parent:WaitForChild("TextBox")
local Players = game:GetService("Players")
local RealName = script.Parent:WaitForChild("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].Name
    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)

The table.find() returns the index, not a value, that’s why it didn’t put the player name.

1 Like

Now it gives me the error of “attempt to concatenate string with nil

1 Like

I already found the solution but now I want the player with the DisplayName to also be found, I’m really looking for it and I don’t see how to do it