How to check a players character exists?

I have a list of players like this:

local gamePlayers = {player1, player2, player3, player4, player5, player6}

How would I check every player insides this lists character exists, and if it doesn’t it keeps checking until it does? Thanks for any help

If you know how to do a loop, then the first part checking if every player’s character exists should be doable.

Then, the hardest part is what to do when you find a player whose character doesn’t exist. You have two options here:

  • Wait for that player’s character to spawn then re-check all players
  • Wait for that player’s character to spawn then check the remaining

If while waiting for a player’s character to spawn that other players could leave, or no longer have their character be in the workspace, then it would make sense to pick the first option. But, this means one player can mess everything up depending on what you’re doing.

The logic behind the first one is something like this in pseudocode:

for each player in gamePlayers
    if player's character does not exist then
        wait until player's character spawns
        restart loop

You can restart the loop via recursion (recalling the function again from inside itself), changing the index variable if you do a for i loop, or however you want.

EDIT: Don’t do a loop of task.wait() to find if a player’s character exists please. That’s called “polling” and is bad practice given you have an existing event. You could do player.CharacterAdded:Wait() if you know the player’s character simply hasn’t spawned in yet.

1 Like
for _, plr in gamePlayers do
    repeat
       task.wait()
    until player.Character ~= nil
end
1 Like

You could filter out the ones that have a character into a new table.

Something like so:

local gamePlayers = {player1, player2, player3, player4, player5, player6}

local function CheckPlayers()

local HaveCharacter = {}
local MissingCharacter = {}

	for _, gamePlayer in pairs(gamePlayers) do
		if gamePlayer:IsA("Player") then
			if gamePlayer.Character then
				if not table.find(HaveCharacter, gamePlayer) then
					table.insert(HaveCharacter, gamePlayer)
					print("HAVE CHARACTER =", HaveCharacter)
				end
			else
				if not table.find(MissingCharacter, gamePlayer) then
					table.insert(MissingCharacter, gamePlayer)
					print("MISISNG CHARACTER =", MissingCharacter)
				end
			end
		end
	end
end

But, as @colorblindcrayon pointed out, they may not have a character afterwards. They may reset, die or leave the experience.

1 Like

So this is really oversimplified, but I made it barebones so that you have more freedom in how to implement the code for your project:

for i, player in ipairs(gamePlayers) do
	local charloaded = false
	local character = player.Character or player.CharacterAdded:Wait()
	charloaded = true
end
1 Like

Thank you very much for your help. Yes i’m simply waiting for them to respawn so would this work?

                       for i = #gamePlayers, 1, -1 do
							local tempChr =  gamePlayers[i].Character or gamePlayers[i].CharacterAdded:Wait()
						end

I added the or in since some of them dont die, so their character would already be in the workspace.

You could do something like this:

local chars = {} -- stores our instances

for i, chars in pairs(chars) do -- loop through instances

   local path = chars:GetFullName() -- get full path if it exists
   
   if path and path ~= nil then -- if it isnt nil/exists
      print("Exists")
      return path -- return path for future use if needed.
   else
      print("Doesnt exist") -- doesnt exist as path would be nil
   end 
   
end

No need for the variable if you won’t use it.

for i = 1, #gamePlayers do
    if not gamePlayers[i].Character then -- if player has no character
        gamePlayers[i].CharacterAdded:Wait() -- wait for respawn
    end
end

You may want to modify the if-statement to add an “or” with a check for the character’s humanoid state if you want to also wait for dead characters to respawn (characters that are dead are still in the workspace and still exist)

1 Like