Ive been working on a pathfind npc, (while theres been two problems, im deciding to make a post on this one for the pure fact that my other one got ghosted) and the npc kinda… freaks out when they are near the player. This is what is happening:
Heres my code:
task.wait(4)
local ai = script.Parent
local humanoid = ai:WaitForChild("Humanoid")
local torso = ai:WaitForChild("Torso")
local hrp = ai:WaitForChild("HumanoidRootPart")
local pathfind_service = game:GetService("PathfindingService")
local path = pathfind_service:CreatePath({
AgentRadius = 1.5;
AgentHeight = 6;
AgentCanJump = true;
Costs = {
Climb = 2;
}
})
local waypoints
local next_index
local reached
local blocked
local visualize = true
local visualized_waypoints = {}
ai.PrimaryPart:SetNetworkOwner(nil)
function follow_path(destination)
local success, error_mes = pcall(function()
path:ComputeAsync(hrp.Position, destination)
end)
if visualize then
for i, v in ipairs(visualized_waypoints) do
v:Destroy()
end
table.clear(visualized_waypoints)
end
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
blocked = path.Blocked:Connect(function(index)
if index > next_index then
blocked:Disconnect()
follow_path(destination)
end
end)
if visualize then
for i, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
part.Position = waypoint.Position
part.Shape = Enum.PartType.Ball
part.Material = Enum.Material.Neon
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Color = Color3.fromRGB(255, 255, 255)
table.insert(visualized_waypoints, part)
end
end
if not reached then
reached = humanoid.MoveToFinished:Connect(function(reached_)
if reached_ and next_index < #waypoints then
next_index += 1
humanoid:MoveTo(waypoints[next_index].Position)
if waypoints[next_index].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
else
reached:Disconnect()
blocked:Disconnect()
end
end)
end
next_index = 2
humanoid:MoveTo(waypoints[next_index].Position)
if waypoints[next_index].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
else
warn(error_mes)
end
end
while task.wait() do
follow_path(workspace.scpSCARRYY.Other.LocalCharacterPos.Value.Position)
end
Dont wanna hear anything about “set network owner to nil”, “make npc follow the CLIENTS position using rmeote events waaa” because ive already tried all of those and it doesnt work.
So as doing a magnitude check for the distance between waypoints. NOTHING is working.
The NPC’s behavior could be caused by the path being recomputed too frequently or the NPC getting stuck on obstacles. Try adding a check to see if the player has moved a certain distance before recomputing the path and adjust the AgentRadius , AgentHeight , and Climb properties of the Path object to better match the size of your NPC.
Changed the tick speed of the calculations. Problem got worse.
Changed agent radius, problem still persisted.
Changes agent height, problem still persisted.
Deleted truss cost, problem still persisted.
Turned off visualize waypoints, problem still persisted.
Try adjusting the NPC’s movement speed, AgentRadius , and AgentHeight properties of the PathfindingService to improve its accuracy in reaching its target
local tweenService = game:GetService("TweenService")
-- Inside your follow_path function, after you get the next waypoint:
local nextWaypoint = waypoints[next_index].Position
local distance = (nextWaypoint - hrp.Position).Magnitude
local time = distance / humanoid.WalkSpeed
local tweenInfo = TweenInfo.new(time)
local tween = tweenService:Create(hrp, tweenInfo, {CFrame = CFrame.new(nextWaypoint)})
tween:Play()