For loops not working

  1. What do I want to achieve?
    Working for loops.

  2. What is the issue?
    For loops not working.

  3. What solutions have I tried so far?
    Literally everything.

For some reason, my for loops are not working. I’ve looked everywhere for a solution.

for _, plr in pairs(players) do
		plrpos[#plrpos + 1] = plr.Character.HumanoidRootPart.Position
	end
	for _,i in pairs(plrpos) do
		posmagnitude[#posmagnitude + 1] = (nb.HumanoidRootPart.Position - plrpos).Magnitude
	end
	for _,i in pairs(posmagnitude) do
		minmagnitude = math.min(posmagnitude, i)
	end
	for i,_ in pairs(posmagnitude) do
		if minmagnitude == i then
			TARGET = players[i]
			break
		end
	end

All the tables and variables(assigned in the loops) are nil. The loops do not run at all.

There are no errors other than all variables being nil.

Have you tried doing table.insert() instead of indexing them into the table? I think this is the problem since I have had this sort of problem before.

I assume players is a table of players (ex. game.Players:GetChildren()), and not game.Players, same with all things you are utilizing inside the pairs() call.

Are you sure that they (players, plrpos, posmagnitude) are not nil nor empty?

Your loops aren’t working because you’re not doing them correctly…