Making AI finds Path Instantly When Player dies

How can i make the AI simply finds path easily im using SimplePath module and somehow i can see that if the player spawns it finds path but its little bit slower than you think if you have an idea help.

local SimplePath = require(script.Pathfinding)
local CharacterSize = script.Parent:GetExtentsSize();
local myHum = script.Parent:FindFirstChildWhichIsA('Humanoid')

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	local player = false
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if game.Players:GetPlayerFromCharacter(temp2) and (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

function checkSight(target)
	local ray = Ray.new(script.Parent.HumanoidRootPart.Position, (target.Position - script.Parent.HumanoidRootPart.Position).Unit * 200)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
	if hit then
		if hit:IsDescendantOf(target.Parent) then
			return true
		end
	end
	return false
end

function checkHeight(target)
	if math.abs(script.Parent.HumanoidRootPart.Position.Y - target.Position.Y) < 10.5 then
		return true
	end
	return false
end

function checkDirect(target)
	if checkSight(target) and checkHeight(target) and (script.Parent.HumanoidRootPart.Position - target.Position).Magnitude < 99999999999 then
		return true
	end
	return false
end

local Dummy = script.Parent
local Goal = Vector3.new()
local Path = SimplePath.new(Dummy, {
	AgentRadius = (CharacterSize.X+CharacterSize.Z)/4,
	AgentHeight = CharacterSize.Y, 
	WaypointSpacing = math.huge,
})

Path.Visualize = true

Path.Blocked:Connect(function()
	Path:Run(Goal)
end)

Path.WaypointReached:Connect(function()
	Path:Run(Goal)
end)

Path.Error:Connect(function(errorType)
	Path:Run(Goal)
end)

coroutine.wrap(function()
	game:GetService("RunService").Stepped:Connect(function()
		local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
		if target then
			if checkDirect(target) then
				myHum:MoveTo(target.Position)
				Path:Stop()
				Goal = nil
			elseif checkSight(target) and math.abs(script.Parent.HumanoidRootPart.Position.X - target.Position.X) < 1 and
				math.abs(script.Parent.HumanoidRootPart.Position.Z - target.Position.Z) < 1 then
			else
				Goal = target.Position

				Path:Run(Goal)

			end
		end
	end)
	coroutine.yield()
end)()
1 Like

When you say slower do you mean the speed it finds a path or the npcs speed? If its the NPCs speed then up the walkspeed in its humanoid. Otherwise while I don’t know much about SimplePath why use that rather than roblox’s pathfinding service? Perhaps SimplePath is slower than Roblox’s?

Not the NPC’s Speed its actually fine i mean by finding path when its blocked by raycasts
if im correct. And yes i foun SImplePath is simple to code for pathfinding i dont know much about pathfinding yet

what im saying after Player dies he goes for another player isnt moving for a little bit

Edit: it does instantly finds path after player dies going to another player most of the time

or maybe a problems with raycasts?

turns out i have to do Path:Run(Target.Position)
and Delete the part with
Path.Error:Connect(function()
local target = findTarget(script.Parent.HumanoidRootPart.Position)
Path:Run(target.Position)
end)

it does slightly better than before