Basically, my AI walks around perfectly, no stuttering or anything. When it senses a player nearby, it pathfinds to the player, it all looks smooth and clean, but as soon as the AI gets within like 6 studs of the player, it starts to stutter. I’ve set the NetworkOwnership of ALL parts within the monster to server, and I just can’t find anything about fixing this. It’s ONLY when he gets near the player he does it.
I’m working on a big game and I really need help with this problem. I know this script is kind of big, but I just can’t figure out why he is stuttering.
local char = script.Parent
local hum = char.Humanoid
local rep = game:GetService("ReplicatedStorage")
local changeMusic = rep:FindFirstChild("chaseMusic")
local waypoints = workspace.Waypoints:GetChildren()
local PathfindingService = game:GetService("PathfindingService")
local runSpeed = 35
local walkSpeed = 20
local pastTarget
local lostTarget = true
local function canSeeTarget(target)
local origin = char.Torso.Position
local direction = (target.HumanoidRootPart.Position - char.Torso.Position).unit * 120
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, char)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 60
local scareDist = 15
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local dist = (char.Torso.Position - target.HumanoidRootPart.Position).Magnitude
if dist < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = dist
elseif dist < scareDist then
scareDist = dist
nearestTarget = target
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
AgentHeight = 3,
AgentRadius = 2,
AgentCanJump = false,
AgentCanClimb = false,
Costs = {
Walls = math.huge
}
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(char.Torso.Position, destination.Position)
return path
end
--THIS IS WHERE HE WALKS TO THE PLAYER
local function attack(target)
local dist = (char.Torso.Position - target.HumanoidRootPart.Position).Magnitude
print("attac")
if dist > 7 then
local path = getPath(target.HumanoidRootPart)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
print("FINDING PLAYER")
hum.WalkSpeed = runSpeed
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
else
hum:MoveTo(target.HumanoidRootPart.Position - char.Torso.CFrame.LookVector * 10)
end
else
target.Humanoid.Health = 0
if target.Humanoid.Health <= 0 then
rep.killPlayer:FireClient(game.Players:GetPlayerFromCharacter(target))
char.Head.Kill:Play()
char.Head.Kill2:Play()
end
print("Killed ", target.Name)
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target then
print("TARGET FOUND - ", target.Name)
if target and target.Humanoid.Health > 0 then
hum.WalkSpeed = runSpeed
attack(target)
--walkTo(attack())
pastTarget = game.Players:GetPlayerFromCharacter(target)
lostTarget = false
changeMusic:FireClient(game.Players:GetPlayerFromCharacter(target), true)
end
break
else
print("Patrolling")
hum.WalkSpeed = walkSpeed
if pastTarget and lostTarget == false then
print(pastTarget.Name)
lostTarget = true
changeMusic:FireClient(pastTarget, false)
print("Fired Stop")
pastTarget = nil
end
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
end
else
hum:MoveTo(destination.Position - char.Torso.CFrame.LookVector * 10)
end
end
local function patrol()
local waypoints = workspace.Waypoints:GetChildren()
walkTo(waypoints[math.random(1, #waypoints)])
end
while true do
task.wait(0.1)
patrol()
end
This one guy had another post on it, and had a really good video of what is also happening to me, but I can’t figure out how he fixed it. Here is the video of what is happening for me and him.
Post: Pathfinding AI stutters when it nearly approaches player
Video:
See how the AI is smooth until it gets close to the player, most of the time not even REACHING the player at all!