I am making an NPC, and for some reason when it has higher speed than the player it starts to stutter. all of the parts in the character network owner are set to nil.
The players Sprint speed is 30 whilst the monsters is 37.5
I didn’t make the original code, but I modified it a bit.
vid of issue:
Code of main pathfinding loop:
while task.wait() do
local enemytorso = GetTorso(HumanoidRootPart.Position)
if enemytorso ~= nil then -- if player detected
Target = true
Humanoid.WalkSpeed = NPC_Settings.ChaseSpeed
if enemytorso.Parent:FindFirstChild("_Chasing") == nil then
local Value = Instance.new("NumberValue",enemytorso.Parent)
Value.Name = "_Chasing"
Value.Value = 1
else
enemytorso.Parent:FindFirstChild("_Chasing").Value = 1
end
isWandering = 1
local function checkw(t)
local ci = 3
if ci > #t then
ci = 3
end
if t[ci] == nil and ci < #t then
repeat
ci = ci + 1
task.wait()
until t[ci] ~= nil
return Vector3.new(1, 0, 0) + t[ci]
else
ci = 3
return t[ci]
end
end
path = Pathfinding:ComputeAsync(HumanoidRootPart.Position, enemytorso.Position)
waypoint = Pathfinding:GetWaypoints()
oldpoints = waypoint
local connection;
local direct = Vector3.FromNormalId(Enum.NormalId.Front)
local ncf = HumanoidRootPart.CFrame * CFrame.new(direct)
direct = ncf.p.unit
local rootr = Ray.new(HumanoidRootPart.Position, direct)
local phit, ppos = game.Workspace:FindPartOnRay(rootr, HumanoidRootPart)
local Distance = (math.ceil((HumanoidRootPart.Position - enemytorso.Position).magnitude))
if NPC_Settings.Lunge and Distance < NPC_Settings.MiniumLungeDistance and Humanoid.PlatformStand == false then
Humanoid.PlatformStand = true
local LinearVelocity = HumanoidRootPart.Attachment.LinearVelocity
LinearVelocity.Enabled = true
LinearVelocity.VectorVelocity = (-(HumanoidRootPart.Position - enemytorso.Position) * NPC_Settings.LungeIntensity) + NPC_Settings.LungeOffset
if HumanoidRootPart:FindFirstChild("Lunge") then
HumanoidRootPart.Lunge:Play()
end
task.delay(.1,function() LinearVelocity.Enabled = false end)
task.delay(1,function() Humanoid.PlatformStand = false; Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp) end)
end
if path and waypoint or checkw(waypoint) then
if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
Humanoid:MoveTo( checkw(waypoint).Position )
Humanoid.Jump = false
end
if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump and NPC_Settings.CanJump and Humanoid.Sit == false then
Humanoid.Jump = true
connection = Humanoid.Changed:connect(function()
Humanoid.Jump = true
end)
Humanoid:MoveTo( checkw(waypoint).Position )
else
Humanoid.Jump = false
end
if connection then
connection:Disconnect()
end
else
for i = 3, #oldpoints do
Humanoid:MoveTo(oldpoints[i].Position )
end
end
elseif enemytorso == nil and NPC_Settings.canWander then -- if player not detected
Target = false
isWandering = 0
path = nil
waypoint = nil
end
end