Hey, I want to re-optimize some old and new scripts.
I have been using 2 ipair loops to get the children of workspace, and then I would use another on to get the children of Players, and then I would check if their names are the same. And if they were, I would do whatever I would do after that.
But I want to re-optimize the script and make it look more clean, professional, and use less memory and keep a good FPS for the player.
local players = game:GetService("Players")
local function getCharacters()
local characters = {}
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if typeof(character) == 'Instance' and character:IsA("Model") then
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if typeof(humanoid) == 'Instance' and humanoid:IsA("Humanoid") and humanoid.Health > 0 then
table.insert(characters, character)
end
end
end
return characters
end
Get all of the player characters in your game that are not dead.