See If Player Is In Game

Im trying to make a textbox where you type in the players name if they are in the game it will print “Player is in game” I cant figure out how to do this but I know I’m close I tried this script using the local players name to see if they’re name is what they type.

local players = game.Players:GetPlayers()

local textBox = script.Parent

textBox.ClearTextOnFocus = true

local function onFoucusedLost(enterPressed, _inputObject)
	if enterPressed then
		print("Enter Pressed")
		
		local typed = textBox.Text
		
		local isPlayer = table.Find(players, typed)
		
		if typed == game.Players.LocalPlayer.Name then
			print("This Player Is In Game")
			
		else
			print("This Player Is Not In Game")
			
		end
		
		
	end
	
end

textBox.FocusLost:Connect(onFoucusedLost)



I want to see if ANOTHER player is in the game how do I do this thanks for any help :slight_smile:

Do you mean in game or in the same server as the player is in?

same server also i just realized the code doesn’t work exactly how I wanted it to.

You need to declare your local players inside the function. when you create the variable at the start it is locked-in with what ever players are currently logged in, maybe none. You are close! table.find cannot find by name though so you need to iterate over the players array.

local players = game.Players:GetPlayers()
for _, player in players do
    if player.Name == typed or player.DisplayName == typed then
       return true
    end
end
1 Like

You can do if game.Players:FindFIrstChild(“Player name”) then bla bla bla end

I got class but I’m going to try that later.

1 Like

Use Players:FindFirstChild.

local Game = game
local Script = script
local Players = Game:GetService("Players")
local TextBox = Script.Parent

local function OnFocusLost(EnterPressed, InputObject)
	if not EnterPressed then return end
	local Player = Players:FindFirstChild(TextBox.Text)
	if Player then print(Player.Name) end
end

TextBox.FocusLost:Connect(OnFocusLost)
2 Likes

I wasnt quite sure where to put this code at in the script but Forummer solved it for me thanks for your help anyways.

1 Like

One problem I can’t get it to say if the player is not in game.

if Player then
	print(Player.Name.." is in the game.")
else
	print(TextBox.Text.." is not in the game.")
end
1 Like