Pathfinding Script not following the player and instead moving to the orgin of the place

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to know why my npc is not following my player’s character

  2. What is the issue? Include screenshots / videos if possible! The Npc i programed to follow players is not following them

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I have looked over my script and found nothing wrong with it

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Npc = script.Parent

local NpcHumanoid = Npc:FindFirstChild("Humanoid")

local NpcHumanoidRootPart = Npc:FindFirstChild("HumanoidRootPart")

NpcHumanoidRootPart.Anchored = false

local NpcTorso = Npc:FindFirstChild("UpperTorso")

local AgentPrameters = {
	AgentRadius = 5;
	AgentHeight = 10;
	AgentCanJump = true;
	WayPointSpacing = 3;
	Costs = {
		BasePlate = 100;
		Grass = 0
	}
}

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	
	local Humanoid = Character:FindFirstChild("Humanoid")
	
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	
	local PathFindingService = game:GetService("PathfindingService")
	
	local Path = PathFindingService:CreatePath(AgentPrameters)
	
	Path:ComputeAsync(NpcTorso.Position, HumanoidRootPart.Position)
	
	if Path.Status == Enum.PathStatus.Success then
		print("Path Status is a sucess!")
		local Waypoints = Path:GetWaypoints()
		
		for _,Waypoint in ipairs(Waypoints) do
			local Node = Instance.new("Part")
			Node.Shape = Enum.PartType.Ball
			Node.Size = Vector3.new(0.6,0.4,0.6)
			Node.Position = Waypoint.Position + Vector3.new(0,1,0)
			Node.BrickColor = BrickColor.new("Earth green")
			Node.Material = Enum.Material.Neon
			Node.Anchored = true
			Node.CanCollide = false
			Node.Parent = game.Workspace
			
			if Waypoint.Action == Enum.PathWaypointAction.Jump then
				NpcHumanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			NpcHumanoid:MoveTo(Waypoint.Position)
			NpcHumanoid.MoveToFinished:Wait(2)
		end
	else 
		print("Path status was not sucessfull path has been blocked")
		Path.Blocked:Connect(function(BlockedWaypointIndex)
			print(BlockedWaypointIndex .. "Is being Blocked")
			Path:Check0cclusionAsync(NpcTorso.Position)
		end)
		Path.Unblocked:Connect(function(UnBlockedWaypointIndex)
			print(UnBlockedWaypointIndex .. "Has been unblocked")
		end)
	end
end)

Hello, all so i’m trying to make a advanced pathfinding AI system for my game and i’m trying to make it follow the player when they join but the problem is AI is not following the players and are instead going to the workspace orgin point why is this happening? Thank you all for your help :slight_smile:

It looks like you are only getting their position when they join.
You need to constantly have the NPC checking to see where the player is during the game, not just when they join.
I’m guessing you don’t have a spawn location, or if you do it’s at 0,0,0. Try adding a spawn location at 20,0,20 or move your current spawn to that location to see if that’s where the NPC goes to.