Fix For NPC Targeting

I want to make an NPC go towards the allocated player but I need the goal for the NPC to be, Goal = game.Players.(nearestplayer) but you cannot put brackets in the statement so I cant fix the issue, I’ve tried using tables because I cant do, Goal = game.Players., nearestplayer But they still don’t work :confused:

local Players = game:GetService("Players")

local part = game.Workspace.Harvey.Head

local maxDistance = 10000

while true do
	wait(1)
	local nearestPlayer, nearestDistance
	for _, player in pairs(Players:GetPlayers()) do
		character = player.Character
		distance = player:DistanceFromCharacter(part.Position)
		if not character or 
			distance > maxDistance or
			(nearestDistance and distance >= nearestDistance)
		then
			continue
		end
		nearestDistance = distance
		nearestPlayer = player

		
	end
	print("The nearest player is ", nearestPlayer)
end 


-------------------------------------------------------------------


--Import the module so you can start using it
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)

--Define npc
local Dummy = game.workspace.Harvey

-- Define a part called "Goal"
Goal = game.Players.(nearestplayer)

--Create a new Path using the Dummy
local Path = SimplePath.new(Dummy)

--Helps to visualize the path
Path.Visualize = true


while true do
	Path:Run(Goal)
end

(I’m also using ‘SimplePath’ for this NPC if you need to know)

1 Like

you should make like this
Goal = game.Players[nearestplayer.Name]
Hope that helps

1 Like

Still doesn’t work but thanks for trying :slight_smile:

1 Like
game:GetService(“Players”):FindFirstChild(nearestplayer.Name)

That should work

Where would I put this in the code?

oh so Goal = (the code i did above)

Oh, could you tell us at least what’s wrong? Like nearestplayer doesn’t exist or sth?
I am not keen onto “simulating” your code in my head.

There were no errors on script analysis its that the NPC needs a statement that tells the part of the player to go to

Resonds with errors, sad

Btw you are skipping an iteration if a player’s character does exist, so that is strange a little bit. Are you trying to chase ghosts?
And why are you using findfirstchild if nearestplayer is what you are looking for?
Only that you need to get is a .Character reference…

obviously change the nearestplayer variable to the one you have defined i was on mobile and just quickly typed it

Goal is a table and table needs string or instance not a script.
You can try by just simply put the player name in there nearestplayer.Name to put a string or game.Players[nearestplayer.Name] to put a instance
Sorry if im wrong.

and change the quotation marks so the type checker dosent error

Nah Goal is supposed to be a vector or base part i just checked the simple path api

replace my code again with

Goal = nearestplayer.Character.HumanoidRootPart


Here, take it and don’t ask.

while true do
	wait(1)
	local nearestPlayer, nearestDistance = nil,maxDistance
	for _, player in pairs(Players:GetPlayers()) do
		character = player.Character
		distance = player:DistanceFromCharacter(part.Position)
		if not character or distance >= nearestDistance then
			continue
		end
                
		nearestDistance = distance
		nearestPlayer = player

		
	end
	print("The nearest player is ", nearestPlayer)
    goal = nearestPlayer
end