Pathfinding AI Stops at player

Using a free model killer AI system, I used it to edit it and make it into my own pathfinding module. It works well, but it faces a problem that most pathfinding scripts have when the AI will stop at the player when near them, making it impossible to attack them.

https://gyazo.com/0a5fdb2a3b67a3ffcd5e2aaa96486068

I’ve tried everything I can to try and fix this problem, I’ve even rewritten the module a couple of times to fix it. I’ve searched DevForum for multiple times for similar problems in which I can find them, but most of the solutions are ‘Nevermind, I fixed it.’ or when I use the solution to try and give me an idea on how to fix it, I still can’t do it.

All I want is for the AI to catch up to the player to attack them.

local function walkTo(killer, destination, walkSpeed, sprintSpeed, maxDistance)
	local path = getPath(killer, destination)

	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in pairs(path:GetWaypoints()) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				killer.Humanoid.Jump = true
			end

			local target = findTarget(killer, maxDistance)
			if target then
				capture(killer, target, killer:GetAttribute("Damage"))
				killer.Humanoid.WalkSpeed = sprintSpeed
			else
				killer.Humanoid.WalkSpeed = walkSpeed
				killer.Humanoid:MoveTo(waypoint.Position)
				while killer.Humanoid.MoveToFinished:Wait() == false do
					if (killer.HumanoidRootPart.Position - waypoint.Position).Magnitude < 1 then
						break
					end
				end
			end
		end
	else
		killer.Humanoid:MoveTo(destination.Position - (killer.HumanoidRootPart.CFrame.LookVector * 10))
	end
end
1 Like

My guess is that, because moveTo isn’t called in the if target then statement, and only called to attack, it’s one pathway point behind when it attacks?

I added this line to the if target then statement: killer.Humanoid:MoveTo(target.HumanoidRootPart.Position)

I am still facing the same problem, and the killer still jitters when close to the player.

1 Like

Does capture() do anything to the enemy’s movement? I would like to investigate the place if possible.

local function capture(killer, target, damage)
	local distance = (killer.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	local attackCooldown = killer:GetAttribute("AttackCooldown")
	local attackWait = killer:GetAttribute("AttackWait")

	local currentTime = tick()
	local lastTime = lastAttackTime[killer] or 0

	if distance <= 3 and (currentTime - lastTime) >= attackCooldown then
		playAttackAnimations(killer)

		local weaponName = killer:GetAttribute("WeaponName")
		local weapon = weaponName and killer:FindFirstChild(weaponName)

		if weapon then
			local swingSound = weapon:FindFirstChild("Swing")
			if swingSound and swingSound:IsA("Sound") then
				swingSound:Play()
			end
		end

		wait(attackWait)

		if target.Humanoid and target.Humanoid.Health > 0 then
			target.Humanoid:TakeDamage(damage)
		end

		if weapon then
			local hitSound = weapon:FindFirstChild("Hit")
			if hitSound and hitSound:IsA("Sound") then
				hitSound:Play()
			end
		end

		lastAttackTime[killer] = currentTime
	end

	killer.Humanoid:MoveTo(target.HumanoidRootPart.Position)
end
1 Like

Is it possible to send a copy of the place so I can investigate it?

I can make a place that only uses the module and killer model real quick, and message it to you.

1 Like

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