Hi guys, im trying to code a find nearest target script and apparently i can't properly save my humanoidrootpart properly here's the code(I"ll remove as much redundant code as possible):
function FindNearestTarget()
local list = game.Workspace:GetChildren()
for i=1, #list do
if list[i].ClassName == "Model" and list[i]:FindFirstChild("Humanoid") then
TargetPos = {list[i].HumanoidRootPart.Position}
wait(1)
end
end
if TargetPos then
print (unpack(TargetPos))
end
end--Function End
The above code apparently results in TargetPos to only have 1 element regardless of the number of players in the game. If this function was ran in an infinite loop, and there are 2 players for example, TargetPos seems to replace it's first element with the next player's position, then go back to printing the first player's position. I want to be able to print (TargetPos[2]) but it always result in a nil because it was never saved as the second element in the first place. any idea whats wrong?

2 comments
You're setting the table "TargetPos" instead of adding to it. Define "TargetPos" beforehand and add to it instead. TargetPos[#TargetPos+1] = list[i].HumanoidRootPart.Position
Ahh that seems to be the problem. I've always thought that a table would automatically increment itself upon receiving the data. Thanks a lot, I won't make this same mistake again.