Help with Enemy Pathfinding

I want to create a well-functioning enemy pathfinding ai that goes toward waypoints and chases players, as well as going toward a target’s last location to make it harder to escape.
Shown in the video below:

This currently working pathfinding ai script, though with some flaws, works perfectly fine with a single player. However, another problem becomes apparent with 2 or more players. What should happen is that when the enemy kills a player, it goes after other players if it also spots them. This works… somewhat. There is a very noticeable delay when it happens, which I am not wanting.

At first I thought it could’ve been the added code of the enemy going to the target’s last location, but the issue still persisted after removing that part of the code. I’ve looked in devforums and youtube videos for help, but either it is not what I’m going for, or it just does not work. Currently I am just stuck what might be causing the problem.

--
local entity = script.Parent
local humanoid = entity:WaitForChild("Humanoid")
local PathfindingService = game:GetService("PathfindingService")
entity.PrimaryPart:SetNetworkOwner(nil)
local cs = game:GetService("CollectionService")
lastLocation = nil


local function canSeeTarget(target)
	local maxdistance = entity:GetAttribute("maxdistance")
	local origin = entity.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - entity.HumanoidRootPart.Position).unit * maxdistance

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {cs:GetTagged("IsEntity")} 

	local result = workspace:Raycast(origin, direction, raycastParams)

	if result then
		local hit = result.Instance
		if hit and hit:IsDescendantOf(target) then
			return true
		end
	end

	return false
end



local function findTarget()
	local players = game.Players:GetPlayers()
	local maxdistance = entity:GetAttribute("maxdistance")
	local nearest 

	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (entity.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			if distance < maxdistance and canSeeTarget(target) then
				nearest = target
				maxdistance = distance
			end
		end
	end
	return nearest
end

local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 6,
		["AgentRadius"] = 3,
		["AgentCanJump"] = false
	}
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(entity.HumanoidRootPart.Position, destination.Position)

	return path
end


local function attack(target)
	local runspeed = entity:GetAttribute("runspeed")
	local distance = (entity.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

	if distance > 3 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
		humanoid.WalkSpeed = runspeed
		
		
	else

		target.Humanoid.Health = 0
	end
end

local function WalkTo(destination)
	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local walkspeed = entity:GetAttribute("walkspeed")
			local runspeed = entity:GetAttribute("runspeed")
			local target = findTarget()
			
			

			if target and target.Humanoid.Health > 0 then
				attack(target)
				lastLocation = target.HumanoidRootPart.Position
				break
			end
			if lastLocation ~= nil then
				humanoid:MoveTo(lastLocation)
				humanoid.WalkSpeed = runspeed
				humanoid.MoveToFinished:Connect(function()
					lastLocation = nil
				end)
				
			else
					humanoid.WalkSpeed = walkspeed
					humanoid:MoveTo(waypoint.Position)
					humanoid.MoveToFinished:Wait()
				
			end
			
			end
			
	else
		humanoid:MoveTo(destination.Position - (entity.HumanoidRootPart.CFrame.LookVector * 40))
	end
end

local function patrol()
	for _, child in pairs(cs:GetTagged("waypoint")) do
local waypoints = child:GetChildren()
	local random = math.random(1, #waypoints)
WalkTo(waypoints[random])
end
end

while wait() do
	patrol()
end

I believe there may be a delay happening that causes the issue, but I can’t pinpoint what. Help with this problem would be appreciated!

4 Likes

The delay in your pathfinding AI script when an enemy kills a player and goes after other players is likely due to the fact that the script is not checking for other players frequently enough.

Currently, the script is only checking for other players when the enemy is looking for a new target. This means that if the enemy is already attacking a player, it will not check for other players until it has finished killing that player

2 Likes

Do you know where I could place a check in the script? The canSeeTarget(target) local function is used for checking players, but I’m trying to put its call in the other local functions, like calling it inside the attack local function so it checks while attacking. Though I haven’t seen anything change while doing it.

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