Annoying bug with my player distance script [SOLVED]

This script keeps erroring when it runs if not PlayerDistanceTable[1][1] then return nil end

local NPCHumanoidRootPart = NPC:WaitForChild("HumanoidRootPart")
function returnClosestPlayer()
	local PlayerDistanceTable = {}
	for i,v in pairs(Players:GetPlayers()) do
			table.insert(PlayerDistanceTable,{v.Name,v:DistanceFromCharacter(NPCHumanoidRootPart.Position)})	
		end	
	table.sort(PlayerDistanceTable,function(a,b)
		return a[2] < b[2]
	end)
	print(PlayerDistanceTable)
	if not PlayerDistanceTable[1][1] then return nil end -- apparentley, after this check it stops and still errors, still runs even though it doesnt exist.
	return PlayerDistanceTable[1][1]
end
while true do
	wait(1)
	local TargetPlayer = returnClosestPlayer()
	if TargetPlayer ~= nil then -- checks if it isnt nil 
	print(TargetPlayer)
	end
end

“Attempt to index nil with number” this line: if not PlayerDistanceTable[1][1] then return nil end

2 Likes

you could replace that with if not PlayerDistanceTable[1] and PlayerDistanceTable[1][1] then return nil end
Issue here is that if PlayerDistanceTable[1] doesn’t exist then you’re indexing it with nil.

1 Like

I defined it in a varible right above, but for some reason I guess the script wont recognize it… im kinda confused as to what is going on here.

If there are no players then the table would be empty/no indicies.

It’s returning nil because there are no players when the server has just started…
Add a condition to check if there is more than 0 items in the distances table.

local NPCHumanoidRootPart = NPC:WaitForChild("HumanoidRootPart")
function returnClosestPlayer()
	local PlayerDistanceTable = {}
	for i,v in pairs(Players:GetPlayers()) do
		table.insert(PlayerDistanceTable,{v.Name,v:DistanceFromCharacter(NPCHumanoidRootPart.Position)})	
	end	
	table.sort(PlayerDistanceTable,function(a,b)
		return a[2] < b[2]
	end)
	if #PlayerDistanceTable > 0 then
		return PlayerDistanceTable[1][1]
	end
end
while true do
	task.wait(1)
	local TargetPlayer = returnClosestPlayer()
	if TargetPlayer then -- checks if it isnt nil 
		print(TargetPlayer)
	end
end
1 Like

Thank you so much!!! You fixed my issue

Your script is really hard for me to read, So I’ll just give you this since it is similar and simpler.

local function getNearestPlayer()
	
	local distances = {}
	local players = {}
	
	for i, v in pairs(Players:GetPlayers()) do
		local distance = v:DistanceFromCharacter(NPCHumanoidRootPart.Position)
		
		if distance then
			table.insert(distances, distance)
			players[distance] = v
		end
	end
	
	local nearestPlayer = players[math.min(unpack(distances))]
	
	return if nearestPlayer then nearestPlayer else nil
	
end
1 Like
local Game = game
local Players = Game:GetService("Players")

local function GetClosest()
	local Closest = {Distance = math.huge, Player = nil}
	for _, Player in ipairs(Players:GetPlayers()) do
		local Distance = Player:DistanceFromCharacter(Vector3.new(0, 0, 0)) --Example world position.
		if Distance < Closest.Distance then Closest = {Distance = Distance, Player = Player} end
	end
	return Closest --Dictionary with two fields, 'Distance' and 'Player'.
end