-
What do you want to achieve?
Smooth pathfinding of my NPC. -
What is the issue?
NPC starts stuttering whenever root part anchores or when I get close to it, “SetNetworkOwner” function will not work because it can’t set the network of anchored parts or parts welded to ancored parts. -
What solutions have you tried so far?
I have tried to use “Magnitude” but it didn’t worked, it was still stuttering a bit.
Video about “Server VS Client scrpt” also didn’t helped, so I guess DevForum is my only solution.
local PathfindingService = game:GetService("PathfindingService")
local CollectionService = game:GetService("CollectionService")
-- NPC and Humanoid
local NPC = script.Parent -- Assuming the script is a child of the NPC
local humanoid = NPC:FindFirstChildOfClass("Humanoid")
local root_part = NPC:WaitForChild("HumanoidRootPart")
local not_allowed = "Enemy" or "Helper"
local distance
local Cooldown = false
-- Get all waypoints tagged with "Waypoint"
local waypoints = CollectionService:GetTagged("Waypoint")
-- Function to move NPC to a target
local function moveTo(targetPosition)
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentJumpHeight = 10,
AgentMaxSlope = 45
})
path:ComputeAsync(root_part.Position, targetPosition)
local pathWaypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(pathWaypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(0.25)
--[[repeat
distance = (waypoint.Position - root_part.Position).magnitude
wait()
until
distance <= 4]]
end
else
warn("Path computation failed")
end
end
-- Function to select a random waypoint
local function getRandomWaypoint()
if #waypoints == 0 then
warn("No waypoints found! Ensure parts are tagged with 'Waypoint'.")
return nil
end
return waypoints[math.random(1, #waypoints)]
end
-- Patrol Loop
local function patrol()
while true do
local nextWaypoint = getRandomWaypoint()
if nextWaypoint then
moveTo(nextWaypoint.Position)
end
end
end
-- -- -- Jumpscare
root_part.Touched:Connect(function(Part)
local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
local PlayerHumanoid = Part.Parent:FindFirstChild("Humanoid")
if Player and PlayerHumanoid and not Part.Parent:FindFirstChild(not_allowed) and Cooldown == false then
if PlayerHumanoid.Health ~= 0 then
NPC.State.Value = "Killing"
Cooldown = true
root_part.Anchored = true
PlayerHumanoid.WalkSpeed = 0
wait(0.7)
PlayerHumanoid.Health = 0
wait(2)
Cooldown = false
NPC.State.Value = "Patrolling"
root_part.Anchored = false
end
end
end)
patrol()
I hope someone will be able to help me, I’m struggling for a year now!