How to find an element of a table contained within another table?

Hi there,

I’m new to scripting and to coding altogether, and I would appreciate some help with finding the value of an element contained within a nested table.

As a concrete example of what I need help with, say that I’m scripting a zombie, and I want to tell it to find the target that’s closest to him. In my mind, the logical way to do this would be something like:

  1. Get a table of the children of Workspace
  2. Iterate through it and discard elements that don’t have a Humanoid object
  3. Of the elements that remain, iterate through them and get the distances from each Torso object to the zombie’s torso
  4. Save the torso that is closest and tell the zombie to pursue it

The problem I’m having is that I can’t get a for loop to do step 3 exactly as I want. If I write something like

local children = game.Workspace:GetChildren()
for i,child in pairs(children) do
    local humanoid = child:FindFirstChild("Humanoid")
    if humanoid and humanoid.Parent ~= zombie then
        local torso = child:FindFirstChild("Torso")
	    nearestTorso = math.min(torso.Position.magnitude - zombie.Torso.Position.magnitude)
        print(nearestTorso)
    end
end

multiple values are returned, because math.min is being done for each torso separately. Again, what I need is a way to say “for all these torsos that were found, find the one that’s closest to the zombie’s.” How would I go about doing that? I imagine that the answer is pretty simple – any extra insights that you can provide about for loops, tables etc. in general would be super helpful!

1 Like

Hello!
I’m writing this on my phone so correct me if I’m wrong, but I think you can try something like:

local min=math.huge
for i,child in pairs(game.workspace:GetChildren())
local humanoid=child:FindFirstChild('Humanoid')
if humanoid~=nil then
local torso = child:FindFirstChild('Torso')
if torso ~= nil then
local val=( torso.position-zombie.torso.position).magnitude
if val<min then
min=val
local target=torso
end

end
end
end
print (min) --should print the lowest distance for example 10 or 223 

In addition to that you can make a function and pass the closest target as argument.

local function Do_Something(hooman)
--here you can, for example hurt the target or something
end
Do_Something(target) -- the target we found earlier

1 Like

Hi Ancient,

Thanks, this works! I remember seeing this in a free model zombie script, but I thought there was a better way to do it. If this is the best way, I’ll stick with this method.

One question I have: why exactly does this work? val < min is true for every val, so why does it pick the lowest one?

1 Like

I think this can be made with more complicated methods not necessarily efficient but since you said you’re getting started I chose to keep it simple.
This is how it works:
We can divide the ‘for’ loop into as many instructions as there are children in workspace ( the one we’re looking for is a valid torso).
We start with a value that is ‘math.huge’ and we compare it with all distances we find between the zombie and the humanoid.
Every time the min value will remember the shortest distance.
Here’s some more explanation of the algorithmIMG_20190428_205121

1 Like

That makes sense. Thanks for the explanation!