How to get the nearest player from a part/npc?

How would I be able to get the player that is the closest to a part? (in this case humanoidrootpart)

I want to save the player as a variable, because I am planning on implementing it into the pathfinding script I am currently working on. I might also add a range, but that’s for another time. Please send some help as I cannot finish my script without this. I have looked through countless other posts, but none seem to work for me.

Thanks in advance.

1 Like

https://devforum.roblox.com/t/find-the-nearest-player/1258855

3 Likes

doesn’t work because i want it as a variable, if i place my script inside of the loop, it bugs. i tried that one before

2 Likes

so you want to hold the nearest player’s position as a variable?

2 Likes

if possible yes, i tried placing my code in a loop before and it stopped working for some reason.

2 Likes

Can you show the code you are working with,and perhaps we can find the error?

1 Like

well, that is quite simple. If you look at the post I’ve provided above it shows how you can get the nearest player, and to get their position, you could do:

local nearestPlayerPos = player.Character.HumanoidRootPart.CFrame
                                or
local nearestPlayerPos = player.Character.HumanoidRootPart.Position
2 Likes

yes, so i believe it is not possible to do it without a loop?

2 Likes

i don’t understand what your issue is, mind putting your code here?

3 Likes

Just use a reuseable function:

local Players = game:GetService("Players")

local function nearestPlayerToPart(part: BasePart): Player?
	local nearestPlayer = nil
	local nearestDistance = math.huge
	
	for _, player in ipairs(Players:GetPlayers()) do
		local character = player.Character
		
		if character  then
			local rootPart = character.PrimaryPart
			
			if rootPart then
				local distance = (part.Position - rootPart.Position).Magnitude
				
				if distance < nearestDistance then
					nearestPlayer = player
					nearestDistance = distance
				end
			end
		end		
	end
	
	return nearestPlayer
end

nearestPlayerToPart(Instance.new("Part", workspace))
2 Likes
local Players = game:GetService("Players")

local rs = game:GetService("ReplicatedStorage")
local forbidden = rs:WaitForChild("Forbidden")

local ai = require(forbidden:WaitForChild("AI"))
local NPC = script.Parent
local target = workspace.dest

wait(2)
ai.SmartPathfind(NPC,playerhere,true,{Visualize = true, Tracking = true})

this just uses an api from a youtube video, so theres not much to see.
‘ai.smartpathfind’ function is what bugs when i put it in a loop. and in playerhere is going to be the variable if possible.

2 Likes

does this allow me to use the nearestPlayer in another function?

1 Like

Yes, because the nearest player is being returned from the function.

local nearestPlayer = nearestPlayerToPart(Instance.new("Part", workspace))

if nearestPlayer then
    -- there might not even exist a nearest player.
    -- make sure to validate it before doing anything.
end
2 Likes

could you show me how this would work? i don’t really know how the return function works.

this is my function:

ai.SmartPathfind(NPC,playerhere,true,{Visualize = true, Tracking = true})

i want it to be something like this:

local nearestPlayer = nearestPlayerToPart(NPC.HumanoidRootPart))

if nearestPlayer then
       ai.SmartPathfind(NPC,nearestPlayer,true,{Visualize = true, Tracking = true})
end

(the parthfinding works perfectly without it being in a loop. but i think a loop is required to constantly get the closest player)

2 Likes

Looks fine to me. Since we want the pathfinding to be constant (running at all times), we do wrap it in a while loop:

while true do
	local nearestPlayer = nearestPlayerToPart(NPC.HumanoidRootPart))

	if nearestPlayer then
    	ai.SmartPathfind(
			NPC,
			nearestPlayer,
			true,
			{ Visualize = true, Tracking = true }
		)
	end
    task.wait(0.1) -- you *might* want to add a cooldown for each look up.
end
3 Likes

it already is constant, but i’ll try it out.

2 Likes

it works, thank you so much : )

one issue though, my pathfinding seems to be laggier than before, i’ll work on fixing that

2 Likes

Likely because the path is being recalculated a bunch of times a second. I’m fairly sure SimplePath optimizes this, though. So you might want to use that.

3 Likes

tried it in 2 player test, but it doesn’t switch when i come closer to the npc.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.