This is an npc that moves directly towards a player in a given area. For some reason it can reach 500 ping when the npc is chasing the player for a long enough time. Here’s the code. If you can help figure out why its lagging that would be great. thanks. Ignore the commented out part.
--Del later
local Range = 30
local player = game.Players.LocalPlayer
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local hum:Humanoid = char.Humanoid
hum.Died:Connect(function()
for _, v in pairs(workspace.Mobs:GetChildren()) do
if v.Name == player.Name then
v:Destroy()
end
end
end)
for _, v in pairs(workspace.Mobs.Range:GetChildren()) do
task.spawn(function()
local newNpc = game.ReplicatedStorage.BarryTech:Clone()
newNpc.HumanoidRootPart.CFrame = v.CFrame * v.PivotOffset
newNpc.Parent = workspace.Mobs
newNpc.Name = player.Name
local originalPos = newNpc.HumanoidRootPart.Position
local roaming = false
task.spawn(function()
local Animation = "rbxassetid://16014911588"
local newAnim = Instance.new("Animation")
newAnim.AnimationId = Animation
local playingAnim = newNpc.Humanoid.Animator:LoadAnimation(newAnim)
newNpc.Humanoid.Running:Connect(function(speed)
if speed > 0 then
if not playingAnim.IsPlaying then
playingAnim:Play()
print("playing anim")
end
else
playingAnim:Stop()
end
end)
end)
local roamingCoroutine = coroutine.create(function()
task.spawn(function()
while true do
local check = Instance.new("Part", workspace)
check.Position = (newNpc.HumanoidRootPart.CFrame *CFrame.new(math.random(0, 22), 0, math.random(0, 22))).Position
check.Name = math.random(11111, 999999)
local partsInRange = workspace:GetPartsInPart(v)
if table.find(partsInRange, check) then
newNpc.Humanoid:MoveTo(check.Position)
check:Destroy()
task.wait(3)
else
check:Destroy()
end
task.wait()
end
end)
end)
coroutine.resume(roamingCoroutine)
v.Touched:Connect(function(touchingObj)
local ifPlayer = false
if touchingObj.Parent == player.Character then
ifPlayer = true
end
if not ifPlayer then return end
print("touching")
local inside = true
task.spawn(function()
while inside do
newNpc.Humanoid:MoveTo(hrp.Position)
print("moving to character")
task.wait(100)
end
end)
task.spawn(function()
while true do
if not table.find(workspace:GetPartsInPart(v), touchingObj) then
inside = false
coroutine.resume(roamingCoroutine)
break
end
task.wait(0.2)
end
end)
end)
end)
--[[repeat
if (originalPos - hrp.Position).Magnitude <= Range then
newNpc.Humanoid:MoveTo(hrp.Position)
else if roaming == false then
newNpc.Humanoid:MoveTo(originalPos)
if (newNpc.HumanoidRootPart.Position - originalPos).Magnitude < 2 then
roaming = true
end
else if roaming == true then
repeat
newNpc.Humanoid:MoveTo((CFrame.new(originalPos) * CFrame.new(math.random(1,Range), math.random(1,Range), math.random(1,Range))).Position)
if (originalPos - hrp.Position).Magnitude <= 30 then
roaming = false
else
for i=0, 3, 0.5 do
if (originalPos - hrp.Position).Magnitude > 30 then
task.wait(0.5)
end
end
end
until roaming == false
end
end
end
task.wait(0.1)
until player.Character == nil]]
end