Checking if a Player is Still in-game

I have this code:

if(game.Players["Aka_Ethan110"]) but I get this error: attempt to call a nil value

Does anyone know why?

2 Likes

You can just use :FindFirstChild(PlayerName) instead and if it returns nil then that player is no longer in the server.

if game.Players:FindFirstChild("Aka_Ethan110") == nil then --Will execute the code inside if block if :FindFirstChild("Aka_Ethan110") returns nil.
--code
end
4 Likes

try using game.Players:FindFirstChild() or get all the players using :GetPlayers() and loop through them and check if there is a player

1 Like

Thanks a lot, this helped! :))

You should actually is :FindFirstChild(“Aka_Ethan119”,false)
exchange of == nil, it’s better and cleaner
@BenMactavsin

1 Like

In addition to that, you can also do

if game.Players:FindFirstChild("Aka_Ethan110") then
--code
end

This will execute if he is still in the game.

Quick explanation if you don't understand it

So you may ask why there is no == or ~=.
Basically, an if statement checks if it’s true, so if you do

game.Players:FindFirstChild("Aka_Ethan110")

it will return the player.

Another example:

local bool = true

if bool then
   print("The bool is true!")
end
8 Likes

Actually, :FindFirstChild() will either return the instance found or nil in the case nothing was found.

2 Likes
local function CheckPlayer(playername)
    player = game.Players:FindFirstChild(playername)
    if player then
       isingame = true
       return isingame
    else
        isingame = false
        return isingame
    end
end
3 Likes

Oh, yes, it returns the instance of course, thanks for pointing out the mistake. Edited.
I was kinda just writing this without thinking about it lmao.

2 Likes