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