Pathfinding not working

Basically I’m making a system that spawns zombies and handles their pathfinding however my pathfinding code will not work I’m getting no error messages and through some print statements there seems to be something wrong with the waypoints anyone know what it is?

local Zombie = {}
Zombie.__index = Zombie

function Zombie.new(position)

	local self = setmetatable({}, Zombie)
	
	self.Model = game.ServerStorage.ZombieModel:Clone()
	self.Model.Name = "Zombie"
	self.Model.Parent = game.Workspace
	
	self.health = 100
	self.speed = 8
	self.damage = 10
	self.state = "Idle"
	
	print(self.Model)
	
	return self
end

function Zombie:Update()
		--What to do depending on the Zombies state
		
		if self.state == "Idle" then
			
			local ReconPoints = game.Workspace.ReconPoints:GetChildren()
			local MaxDistance = 50
			local ReconTable = {}

			--Get all ReconPoints near it and choose one to randomly go to 
			for _, ReconPoint in pairs(ReconPoints) do
				local DistanceFromReconPoint = (ReconPoint.Position - self.Model.HumanoidRootPart.position).Magnitude
				if DistanceFromReconPoint < MaxDistance then
					table.insert(ReconTable, ReconPoint)
				end
			end
			
			print(ReconTable)

			local ReconPointPickedNum = math.random(1, #ReconTable)
			local ReconPointPicked = ReconTable[ReconPointPickedNum]

			local path = game:GetService("PathfindingService"):CreatePath()
		print(ReconPointPicked)
		print(self.Model.HumanoidRootPart.position)
		print(ReconPointPicked.Position)

			path:ComputeAsync(self.Model.HumanoidRootPart.position, ReconPointPicked.Position)
		--If path is possible then go to each waypoint only start gong to the next one when your 5 studs away from current one
			if path.Status == Enum.PathStatus.Success then
				

				for _, waypoint in pairs(path:GetWaypoints()) do
	
					self.Model.Humanoid:MoveTo(waypoint.Position)
					if waypoint.Action == Enum.PathWaypointAction.Jump then
						self.Model.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
					end
					local goal = false

					while goal == false do
						task.wait(0.1)

						local GoalDistance = (self.Model.HumanoidRootPart.position - waypoint.Position).Magnitude
						print(GoalDistance)

						if GoalDistance < 5 then
                       
							goal = true
						end
					end
				end
			else
				print("Path not computed")
			end



		elseif self.state == "Chasing" then
			-- Implement chasing behavior
		elseif self.state == "Attacking" then
			-- Implement attacking behavior
		end
		print("Not Something")
end

return Zombie

Could you please provide the current pathfinding script you’re using?

I did. It’s in the the function Zombie:Update

my bad, on mobile and I didn’t notice the scroll

Can you give us exactly what the output says? It would be nice to know exactly where it goes wrong.

well when I print the chosen location that it’s supposed to go to it prints out a randomly selected ReconPoint which is good. When I print out the waypoints they seem to be fine. when I actually run the code the npc just does nit move so Idk I’m stumped.

I’ve noticed that you are not positioning your zombie model. And is your zombie model properly setup (with all it’s joints) and unanchored?

Edit: Avoid using deprecated properties as well. Use Position instead of position for your HumanoidRootPart.

1 Like

The zombie model is fine and I wont try explaining why I was using position instead of Position but the zombie still does not work

Update: Just to test I put the script in a regular rig and it works after a bit more debugging I think I may have found the problem Edit I’m going to make a new topic to try and get some more help.

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