I have this code:
if(game.Players["Aka_Ethan110"])
but I get this error: attempt to call a nil value
Does anyone know why?
I have this code:
if(game.Players["Aka_Ethan110"])
but I get this error: attempt to call a nil value
Does anyone know why?
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
try using game.Players:FindFirstChild() or get all the players using :GetPlayers() and loop through them and check if there is a player
Thanks a lot, this helped! :))
You should actually is :FindFirstChild(“Aka_Ethan119”,false)
exchange of == nil, it’s better and cleaner
@BenMactavsin
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.
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
Actually, :FindFirstChild()
will either return the instance found or nil
in the case nothing was found.
local function CheckPlayer(playername)
player = game.Players:FindFirstChild(playername)
if player then
isingame = true
return isingame
else
isingame = false
return isingame
end
end
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.