I have an pathfinding npc that chases the player and knocks them back. But sometimes, the pathfinder agent glitched around and also is slower than the player, despite the walkspeed being the same. Also, the knockback is not being applied correctly, and a bit exaggerated. The knockback accelerates, and also stays for way too long. I am using simplepath, an repetitive pathfinding module. Here is the said module:
here is my code:
--local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--local RunService = game:GetService("RunService")
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
--local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")
local raiderStats = require(ServerStorage.RaiderStats)
local simplepath = require(ReplicatedStorage.Packages.simplepath)
local raider = {}
local janitor = require(ReplicatedStorage.Packages.janitor).new()
local function findNearestPlayer(position)
local nearestPlayer, nearestDistance
local maxDistance = math.huge
for _, player in pairs(Players:GetPlayers()) do
local character = player.Character
local distance = player:DistanceFromCharacter(position)
if not character or distance > maxDistance or (nearestDistance and distance >= nearestDistance) then
continue
end
nearestDistance = distance
nearestPlayer = player
end
return nearestPlayer.Character
end
raider.spawn = function(name, spawnIndex)
local model = ReplicatedStorage:WaitForChild("Raiders")[name]:Clone()
local map = Workspace:WaitForChild(ReplicatedStorage.Map.Value)
local BoundingBox = model:GetBoundingBox()
model:PivotTo(map.raidSpawns[tonumber(spawnIndex)].CFrame + Vector3.new(0, BoundingBox.Y / 2, 0))
model.Parent = map.raiders
local modelStats = raiderStats.get(name)
model.Health.Value = modelStats["health"]
local targetUpdater = nil
local target = nil
--local targetdistance = nil
local AgentPathfinding = {
["WaypointSpacing"] = 1.2,
}
local Path = simplepath.new(model, AgentPathfinding, {
TIME_VARIANCE = 0.001,
COMPARISON_CHECKS = 0.05,
JUMP_WHEN_STUCK = true,
})
targetUpdater = task.spawn(function()
while true do
target = findNearestPlayer(model.PrimaryPart.Position)
task.wait()
end
end)
Path.Visualize = true
local damageConnection
--If the path is blocked
damageConnection = RunService.Heartbeat:Connect(function()
local magnitude = (model.PrimaryPart.Position - target.PrimaryPart.Position).Magnitude
print(magnitude)
if magnitude <= 6 then
print("damage")
local targetPrimary:BasePart = target.PrimaryPart
local speed = 150
local velocityForce = model.PrimaryPart.CFrame.LookVector * speed
targetPrimary:ApplyImpulse(velocityForce)
end
end)
local healthConnection = model.Health:GetPropertyChangedSignal("Value"):Connect(function()
if model.Health.Value <= 0 then
damageConnection:Disconnect()
task.cancel(targetUpdater)
model:Destroy()
end
end)
janitor:Add(healthConnection)
while true do
Path:Run(target.PrimaryPart)
end
end
return raider
Here is a video showcasing the bug: