hello ! someone know why my npc moving so jitter , and how to fix it !
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local PathFindingService = game:GetService("PathfindingService")
local NPCSetting = require(script.Parent:WaitForChild("NPC"))
function NoPath(hum,humrp)
local Rand = math.random(-10,10)
local needtoreach = humrp.Position + Vector3.new(Rand,0,Rand)
local path2 = PathFindingService:CreatePath()
path2:ComputeAsync(humrp.Position , needtoreach)
local wp1 = path2:GetWaypoints()
for i ,v in pairs(wp1) do
hum:MoveTo(v.Position)
end
end
function CreatePath(target,demonroot,demonhum)
local Path = PathFindingService:CreatePath()
Path:ComputeAsync(demonroot.Position , target.Position)
local wp = Path:GetWaypoints()
if Path.Status == Enum.PathStatus.Success then
for i ,waypoint in pairs(wp) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
demonhum.Jump = true
end
local distance
local waypointPosition = waypoint.Position
demonhum:MoveTo(waypointPosition)
repeat
distance = (waypointPosition - demonroot.Parent.PrimaryPart.Position).magnitude
wait()
until
distance <= 5
end
else
NoPath(demonhum,demonroot)
end
end
while true do
RunService.Heartbeat:Wait()
for i ,v in pairs(CollectionService:GetTagged("LowRankDemon")) do
if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
local Hum = v.Humanoid
local HumRP = v.HumanoidRootPart
HumRP:SetNetworkOwner(nil)
local Animate = v.Animate
if Animate.Disabled == true then
Animate.Disabled = false
else
--do nothing
end
for i ,enemy in pairs(game.Workspace.Livings:GetChildren()) do
if enemy:FindFirstChild("Humanoid") and enemy:FindFirstChild("HumanoidRootPart") then
local enemyhum = enemy.Humanoid
local enemyhumrp = enemy.HumanoidRootPart
local distance = (enemyhumrp.Position- HumRP.Position).Magnitude
if distance <= NPCSetting["Demon"].Property.Range then
CreatePath(enemyhumrp,HumRP,Hum)
else
NoPath(Hum,HumRP)
end
end
end
end
end
end
heres my npc:
https://gyazo.com/e58da544ded947cb4f90db87137151a9