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.
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
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?
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