I am currently working on a Pathfinding system where an entity chases you and when your close it enough it kills you. Everything is going well except the walking animation sometimes bugs out when it’s chasing you, any ideas?
Script:
--Services--
local RunService = game:GetService('RunService')
local PathfindingService = game:GetService('PathfindingService')
--Variable--
local hum = script.Parent.Humanoid
local humrp = script.Parent.HumanoidRootPart
local Animator = hum.Animator
--Pathfinding
script.Parent.PrimaryPart:SetNetworkOwner(nil)
local destination = workspace.Part.Position
local function findTarget()
local players = game.Players:GetPlayers()
local MaxDistance = 40
local nearestTarget
for i,v in pairs(players) do
if (v.Character) then
local target = v.Character
local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude
if (distance < MaxDistance) then
nearestTarget = target
MaxDistance = distance
end
end
end
return nearestTarget
end
local function agro(target)
local distance = (humrp.Position - target.HumanoidRootPart.Position).Magnitude
if (distance > 8) then
hum:MoveTo(target.HumanoidRootPart.Position)
else
target.Humanoid.Health = 0
end
end
local function createPath(destination)
local path = PathfindingService:CreatePath()
path:ComputeAsync(humrp.Position, destination)
return path
end
local function walkTo(destination)
local path = createPath(destination)
local waypoints = path:GetWaypoints()
for i,v in pairs(waypoints) do
local target = findTarget()
if (target) then
agro(target)
break
end
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
end
end
while task.wait(0.5) do
walkTo(destination)
end
Example: