I Need help SimplePath / SimplePath Pathfinding module not working properly

Hello, I am currently using the “SimplePath” Pathfinding module for zombies in an upcoming zombie game I am working on and the module doesn’t work as intended.

Sometimes the Zombies go back and forth, or always try to chose the shortest possible path that doesn’t work leading to them getting stuck.

image

The Zombies are being controlled in a ServerScript in ServerScript Service.

I store the spawned zombies in a table and the code I am using to go to the player is below:

function GoTo(Object)
	if Object ~= nil then
	--	print(Object)
		if Object:FindFirstChild("Humanoid")  then
			if Object:FindFirstChild("Humanoid").Health > 0 then
				local nearestPlayer, nearestDistance
				local Targets = Characters:GetChildren()
				local Path
				local Blocked
				
				for i = 1, #Targets do
					if Targets[i].ClassName == "Model" then
						if Targets[i]:FindFirstChild("CharacterFolder") then
							if Players:FindFirstChild(Targets[i].Name) then
								if Targets[i].CharacterFolder:FindFirstChild("Values"):FindFirstChild("Downed").Value == false and not Targets[i]:FindFirstChild("IsZombie") then
									local distance = (Object.PrimaryPart.Position.Magnitude - Targets[i].PrimaryPart.Position.Magnitude)--Zombie.PrimaryPart:DistanceFromCharacter(Targets[i].PrimaryPart.Position)
									local character = Targets[i]
									if not character or 
										distance > Range or
										(nearestDistance and distance >= nearestDistance)
									then
										continue
									end
									nearestDistance = distance
									nearestPlayer = Targets[i].PrimaryPart

									if Players:FindFirstChild(Targets[i].Name) then
										Path = SimplePath.new(Object)
										
										--Path.Visualize = true
										Path._settings.JUMP_WHEN_STUCK = true
										
										
										Path:Run(Targets[i].PrimaryPart)
										
										if Path._status == "Idle" then
											warn("IDLE")
											Path._humanoid.Jump = true
											Path:Run(Targets[i].PrimaryPart)
										elseif Path._status == "Blocked" then
											warn("BLOCKED")
											Path._humanoid.Jump = true
											Path:Run(Targets[i].PrimaryPart)
										end
										
										if Path._events.Error or Path._events.Stopped or Path._events.Blocked then
											Path:Run(Targets[i].PrimaryPart)
										end
									end
								end
							end
						end		
					end
				end
			end
		end
	end	
end

Here is the code that runs the function

game:GetService("RunService").Heartbeat:Connect(function()
	for i,v in Zombies do
		task.wait(1)
		GoTo(Zombies[i])
	end
end)

Any help will be appreciated!

Did you check the output for any errors?

Yes I did, I don’t get errors from the script.

maybe you need to change your code a bit, or try recoding the script or try to search up a tutorial?

No no no, you are running the pairs every frame

Magnitude should not be used like that.

I don’t remember the module that much but you definitely should only use SimplePath.new once! Store it and use Path:Run. You are creating memory leaks. Destroy the path when your zombie dies.

function GoTo(zombie, Object)
	if Object == nil or not Object:FindFirstChild("Humanoid") or Object:FindFirstChild("Humanoid").Health <= 0 then
		return
	end

	local nearestPlayer, nearestDistance --nearestPlayer never been used yet!
	local Targets = Characters:GetChildren() --Make sure these characters are close to zombies or it will create a lot of lag
	local Blocked --never been used!

	for _, character in pairs(Targets) do
		if not character.ClassName == "Model"
			or not character:FindFirstChild("CharacterFolder")
			or not Players:FindFirstChild(character.Name)
			or (character.CharacterFolder:FindFirstChild("Values"):FindFirstChild("Downed").Value ~= false or Targets[i]:FindFirstChild("IsZombie"))
		then --forgot continue here, below should be outside the if statement.
			local distance = (Object.PrimaryPart.Position - character.PrimaryPart.Position).Magnitude--Zombie.PrimaryPart:DistanceFromCharacter(Targets[i].PrimaryPart.Position)
			if not character or --don't know why is it here
				distance > Range or
				(nearestDistance and distance >= nearestDistance)
			then
				continue
			end
			nearestDistance = distance
			nearestPlayer = character.PrimaryPart

			if Players:FindFirstChild(character.Name) then --no, you already checked this above!
				zombie.Path:Run(character.PrimaryPart)
			end
		end
	end
end

Sorry, you have made a lot of mistakes so i can’t fix all of them in my limited time but you should format your code something like this, no need to stack if statements and create chaos