Catching all the humanoids

is there any way I can get all the humanoids that are in the Workspace without using:

for _, Target in pairs(game:GetChildren()) do
if Target:FindFirstChild("Humanoid") then
      local humanoid = Target.Humanoid
      print("Humanoids Found!")
   end
end

or

for _, Target in pairs(game:GetDescendants()) do
if Target:FindFirstChild("Humanoid") then
      local humanoid = Target.Humanoid
      print("Humanoids Found!")
   end
end
2 Likes

Not really. There’s nothing bad with looping through the workspace to get the humanoids.

If you want to get all the player’s Humanoids, then you loop through all the game.Player’s players and get the player’s character’s humanoid.

And I don’t think you would loop through the game to get the Humanoids, only the workspace.

1 Like

Right now you are cycling through the whole game so I would just cycle through the workspace but if you wanted an easier way of doing it then put them all in a folder which would keep it more organized and you would only loop through that one specific folder rather then the whole workspace.

1 Like

causing me a lot of lag looping all my Workspace

Didn’t you ask this exact question on a seperate thread?
This one in question. Why not just wait for a response there instead of making a new topic?

you can use coroutine to reduce lag

this my other topic is talking about another mistake

already tried you can even see in my last topic caused me errors

Also; I see no other way than this; atleast this is the way I use. I do not see how laggy this could be, other than on a low end computer.

It is not a matter of computer power but of lag, there is no way for me to go through my Workspace that has more than 20 thousand parts to find 30 or 20 humanoids

If you have a relatively large amount of parts in your game, there is a simple way to reduce lag, but it might be difficult if you have a lot of foreign scripts in your game.

Humanoids can only be created via scripts, already exist in workspace, or be created when a player’s character is loaded. All three of these can be tracked, which would avoid the use of loops in the first place. You could simply create a global table which can be accessed by any other scripts, then store humanoids in that table and remove them when the humanoids are removed with something like .DescendantRemoving or .AncestryChanged.

For example, storing humanoids from characters can be done by using .CharacterAdded in conjunction with .PlayerAdded. Humanoids which currently exist in the game’s workspace before any servers are created can be located and directly pathed to with a script (e.g. workspace.Village.TownNPC.Humanoid). Humanoids created by other scripts can store these humanoids upon their creation.

If there is too much going on in your game for you to know where all humanoids are coming from, then the easier method would definitely be just looping through workspace with a :GetDescendants(), but as you mentioned, it may cause delays depending on the amount of instances in your game’s workspace.

1 Like

Not really, the other alternate is by using your script to perform loops in minimum efficiently

You could just put your Humanoids in a Folder, then implement checks if they’re valid Models or not

To reduce the delays, use coroutine, It will reduce lag/delays, sorry about the indentation.

 local function GetHumanoid()
         for _, Target in pairs(game:GetChildren()) do
              if Target:FindFirstChild("Humanoid") then
                  local humanoid = Target.Humanoid
                  print("Humanoids Found!")
              end
          end
      end

    local GetHumanoidFunction = coroutine.create(GetHumanoid)

    coroutine.resume(GetHumanoidFunction,--parameters if there is)

This global table could be in a module script?

That should work fine as long as it’s being accessed by server scripts instead of local scripts.

I’m testing your script to see if there are any results here just a second

I don’t exactly know but I think it will reduce lag, it will stop the script from blocking other scripts. That’s what I know about coroutine.

Sry, i don’t know anything about coroutine, can you tell me how I can adapt your script for a touched event?

Maybe something like this?

for i, v in ipairs(players:GetPlayers()) do
    local character = v.Character
    if character then
       local humanoid = character:FindFirstChild("Humanoid")
       print(humanoid)
    end
end

aight, actually, it’s basically the same, make a local function, then just put all the code in there. Once your done just do the coroutine.create and coroutine.resume thing like I did above(put the touch function in the local function)