Hello, when using PathFindingService any ai just stutters and has really bad reaction time.
I tried using SetNetworkOwner() but it didnt work.
Script originally made by elite_hunter11 but modified by me:
local PathfindingService = game:GetService("PathfindingService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local hitbox = character:WaitForChild("Hitbox")
local walk = character.Walk
local attack = character.Attack
walk = humanoid:LoadAnimation(walk)
attack = humanoid:LoadAnimation(attack)
walk:Play()
function getClosestPlayer()
local Players = game.Players:GetPlayers()
local nearestPlayer = Players[1]
if nearestPlayer == nil then return end
if #Players > 1 then
for i = 2, #Players do
if nearestPlayer.Character == nil then
nearestPlayer.CharacterAdded:Wait()
end
if Players[1].Character == nil then
Players[1].CharacterAdded:Wait()
end
local distance1 = (nearestPlayer.Character.HumanoidRootPart.Position - rootPart.Position).Magnitude
local distance2 = (Players[i].Character.HumanoidRootPart.Position - rootPart.Position).Magnitude
if distance1 > distance2 then
nearestPlayer = Players[i]
end
end
end
return nearestPlayer
end
local path = PathfindingService:CreatePath({
["AgentCanJump"] = false,
})
coroutine.wrap(function()
while wait() do
local closestPlayer = getClosestPlayer()
if closestPlayer.Character == nil then
closestPlayer.CharacterAdded:Wait()
end
path:ComputeAsync(rootPart.Position, closestPlayer.Character.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
if closestPlayer.Character.Humanoid.Health > 0 then
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
end)()
local debounce = false
while wait() do
local found = false
local region = Region3.new(hitbox.Position - (hitbox.Size / 2), hitbox.Position + (hitbox.Size / 2))
local parts = game.Workspace:FindPartsInRegion3WithIgnoreList(region, script.Parent:GetDescendants())
local model
for _, part in pairs(parts) do
model = part:FindFirstAncestorOfClass("Model")
if model then
if model:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(model)
if player then
found = true
break
end
end
end
end
if found then
if not debounce then
model.Humanoid.Health -= 10
attack:Play()
debounce = true
wait(1)
debounce = false
end
end
end