-
What do you want to achieve? I want the NPC to catch up to the player without any lagging/stuttering
-
What is the issue? NPC lags/stutters behind player, never catching up, despite having faster walkspeed.
-
What solutions have you tried so far? I’ve tried replacing
while true do
withwhile RS.Heartbeat:Wait() do
but that didn’t work. Setting primary part’s network ownership tonil
? Doesn’t work. I’ve also tried searching for answers, but that didn’t work as well.
script.Parent.PrimaryPart:SetNetworkOwner(nil)
local CanDamage = true
local RS = game:GetService("RunService")
local root = script.Parent.HumanoidRootPart
local SS = game:GetService("ServerStorage")
local SimplePath = require(SS.SimplePath)
local path = SimplePath.new(script.Parent)
local LastTorso
local function findNearestPlayer(Position)
task.wait()
local List = game.Workspace:children()
local Torso = nil
local Distance = 999999
local Temp = nil
local Human = nil
local Temp2 = nil
for x = 1, #List do
Temp2 = List[x]
if (Temp2.className == "Model") and (Temp2 ~= script.Parent) then
Temp = Temp2:findFirstChild("HumanoidRootPart")
Human = Temp2:FindFirstChildOfClass("Humanoid")
if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
if (Temp.Position - Position).magnitude < Distance then
Torso = Temp
Distance = (Temp.Position - Position).magnitude
end
end
end
end
return Torso
end
local thingything
while true do
local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
path:Run(target)
path.Error:Connect(function(errorType)
if path.Status ~= "Idle" then
path:Stop()
end
script.Parent.Humanoid:MoveTo(target.Position)
end)
for i,heh in pairs(script.Parent:GetDescendants()) do
if heh:IsA("Part") and heh.Name ~= ("HumanoidRootPart" or "Left Leg" or "Right Leg" or "Right Arm" or "Left Arm") then
thingything = heh
end
end
local e = thingything.Touched:Connect(function(hit)
if hit.Parent.ClassName ~= ("Accessory" or "Hat") then
if hit.Parent:FindFirstChildOfClass("Humanoid") then
hit.Parent:FindFirstChildOfClass("Humanoid").Health = 0
local torsus = hit.Parent:WaitForChild("Torso")
torsus.Velocity = script.Parent.HumanoidRootPart.CFrame.LookVector * math.random(100,200)
end
end
end)
thingything.TouchEnded:Connect(function()
e:Disconnect()
end)
end
end