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.
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.
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
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
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)