Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

FindClosestPlayer Script not working as expected?

I am trying to make a following npc, but my find closest player script is not working as expected.

It seems that the distance is being set to nil, as when i return distance from my function, it prints nil.

I have a beginner to intermediate understanding of Lua and Roblox, but I have no idea whats going wrong.

Here is my code (from a serverside script, if that helps):

playerList = game.Players:GetPlayers()
part = script.Parent.Torso

function findClosestPlayer(originPart, players, maxDistance)
	local closestPlayer
	local mindistance = maxDistance
	for _, playerToFind in ipairs(players) do
		local distance = (originPart.Position - playerToFind.Character.Torso.Position).magnitude
		if distance < mindistance then
			closestPlayer = playerToFind
			mindistance = distance
		end
	end
	
	if closestPlayer then
		print(closestPlayer)
		return closestPlayer
	end
end

while true do
	playerList = game.Players:GetPlayers()
	wait(5)
	chosenPlayer = findClosestPlayer(part, playerList, 100)
	print(chosenPlayer)
end


2 answers

✓ Accepted answer

Remember to make sure that you're not comparing the distance away from yourself. I'm not sure as to why distance would be appearing nil for you, but I made a quick revision to your script which should solve your problem.

local PlayerService = game:GetService("Players")
local localCharacter = script.Parent
local localRootPart = localCharacter.HumanoidRootPart

local function FindClosestPlayer(inRange)
	local playerDistance, closestPlayer = inRange
	for _, player in pairs(PlayerService:GetPlayers()) do
		local character = player.Character
		local blip = character:FindFirstChild("HumanoidRootPart")
		if blip and character.Parent and character ~= localCharacter then
			local blipDist = (localRootPart.Position - blip.Position).Magnitude
			if blipDist <= inRange and blipDist < playerDistance then
				playerDistance, closestPlayer = blipDist, player
			end
		end
	end
	return closestPlayer, playerDistance
end

while true do
	local closestPlayer = FindClosestPlayer(50)
	print(closestPlayer)
	wait(5)
end

I didn't add much (except for some slight performance boosts), but I tested it in studio and it works perfectly. I also changed Torso to HumanoidRootPart to make it R15 compatible. Let me know if you have any questions.

P.S.

Just some tips I would recommend for future reference:

  • Don't use ipairs - it's slower and should only be used in extremely specific situations

  • Try to avoid using non-local variables

None of these have to do with the issue in your code, but it helps clean it up a bit.

1 comment

0

It worked, but i think it tried to run when the player hadn't loaded, as the second time it printed my name but first it printed nil. so my code could very well have worked

Because it is following itself.

This is a fixed script that is working on R6 and R15.

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 100
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if script.Parent.Humanoid.Health < script.Parent.Humanoid.MaxHealth then
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				local player = game.Players:GetPlayerFromCharacter(human.Parent)
				if player then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
						end
					end
				end
			end
		end
	end
	return torso
end

while true do
	wait(0.1)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.