I want to make in 1 of my games a table with active players, I need to list players into the table making sure they are alive with the character and full health
During tests and using prints I found out that the script inserting the players multi times
–Ouput–
player1(x2)
player2
player1
player3
and etc
Tried to use debouce filter but it inserts only 1 player and ignores others , tried to find a solution on the forum and found nothing really helpful to this situation.
wait(30)-- time to make sure my 3 players are loaded before the script runs
print("Starting")
local activeplayers = {}
local allplayers = game.Players:GetChildren()
for i = 1 ,#allplayers do
if allplayers[i] ~= nil then
local char = allplayers[i].Character
if char ~= nil then
local Humanoids = char:FindFirstChild('Humanoid')
if Humanoids ~= nil and Humanoids.Health > 0 then
table.insert(activeplayers, allplayers[i])
end
--printing the players name to figure out if it's working.
for _,v in pairs(activeplayers) do
print(v)
end
end
end
end
I re-wrote the script using the dictionary method.
wait(30)-- time to make sure my 3 players are loaded before the script runs
print("Starting")
local activeplayers = {}
local allplayers = game.Players:GetPlayers()
for _, player in ipairs(allplayers) do
local char = player.Character
if char ~= nil then
local Humanoids = char:FindFirstChild('Humanoid')
if Humanoids ~= nil and Humanoids.Health > 0 then
activeplayers[player] = true
end
--printing the players name to figure out if it's working.
for p,_ in pairs(activeplayers) do
print(p)
end
end
end
local PlayersService = game:GetService'Players';
local PlayersTable = PlayersService:GetPlayers(); -- don't use GetChildren for Players
local ActivePlayers = {};
for _, Player in next, PlayersTable do
if not ActivePlayers[Player.UserId] and Player.Character and Player.Character:FindFirstChildOfClass'Humanoid' and Player.Character.Humanoid.Health > 0 then
ActivePlayers[Player.UserId] = true;
end;
end;
table.foreach(ActivePlayers, print)
Nevermind, I just realized I left the printing loop nested inside the loop that iterates through the player list.
wait(30)-- time to make sure my 3 players are loaded before the script runs
print("Starting")
local activeplayers = {}
local allplayers = game.Players:GetPlayers()
for _, player in ipairs(allplayers) do
local char = player.Character
if char ~= nil then
local Humanoids = char:FindFirstChild('Humanoid')
if Humanoids ~= nil and Humanoids.Health > 0 then
activeplayers[player] = true
end
end
end
--printing the players name to figure out if it's working.
for p,_ in pairs(activeplayers) do
print(p)
end