-- NPC Behavior
local function handleTarget()
while wait(.1) do
local closestTarget = aggressive and getClosestTarget() or currentTarget
local distanceFromSpawn = (humanoidRootPart.Position - spawnPosition).Magnitude
if closestTarget then
local distanceFromNPC = (humanoidRootPart.Position - closestTarget.Position).Magnitude
if distanceFromNPC > personalRange and currentTarget or (distanceFromSpawn > range and currentTarget == nil) then
print("go back")
print(distanceFromNPC, personalRange)
currentTarget = nil
humanoid:MoveTo(spawnPosition)
moving = true
humanoid.MoveToFinished:Connect(function()
moving = false
end)
attacking = false
elseif distanceFromNPC <= attackRange then
if not attacking then
attacking = true
moving = false
humanoid:MoveTo(humanoidRootPart.Position)
task.spawn(function()
while attacking and closestTarget.Parent do
local currentTime = tick()
if currentTime - lastSkillTime >= skillCooldown then
skillMod:TriggerSkill(rig)
lastSkillTime = currentTime
else
Combat.Punch(rig)
end
task.wait(0.3)
end
end)
end
else
if not attacking then
moving = true
humanoid:MoveTo(closestTarget.Position)
task.wait(0.2)
end
attacking = false
end
end
end
end
spawn(handleTarget)
This is a snippet of (what i believe) is causing some pretty big server issues and is causing my game quite a lot of frame drops / ping spikes
This is definitely due to the fact its being loaded onto 30+ npcs via module.
Would there be any optimisation suggestions to give? (fyi ive tested it individually and it is SPECIFICALLY this snippet that does the damage)
Thanks!