Hi, I am working on creating some admin commands for games that I develop, I am trying to figure out how to get every humanoid in the game, so far I’ve been unsuccessful.
I need the humanoid of every player in the game to add command arguments such as ‘all’ or ‘others’
If this is possible, It would be greatly appreciated to know.
Loop over all players, if it’s only player-controlled humanoids you care about (not NPC humanoids). You could build a map of players to their humanoids, if you need that, or just an array of all humanoids. The following code does both:
local Players = game:GetService("Players")
local humanoidsByPlayer = {}
local arrayOfHumanoids = {}
for _,player in pairs(Players:GetPlayers()) do
local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
allHumanoids[player] = humanoid
table.insert(arrayOfHumanoids, humanoid)
end
end
I think you can try this (this gets all humanoids in the game, including NPCs). This gets all humanoids and their characters (parents) in the workspace and appends them to a list:
local getall = game.Workspace:GetDescendants() -- Get all descendants of workspace
local humanoids = {} -- List of all humanoids
local characters = {} -- List of all characters (humanoid's parents)
for i, v in pairs(getall) do -- Iterate through workspace descendants
if v.ClassName == "Humanoid" then -- See if object is a humanoid
table.insert(humanoids, v) -- Append humanoid to list
table.insert(characters, v.Parent) -- Append character to list
end
end