NPC Optimisation (ping issues / frames)

-- 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!

1 Like

Don’t use humanoids, instead use body movers or constraints or lerping, humanoids have a lot of unnecesary states that might cause your issues, another thing would be handling visuals on client, soo animations and entity models

Could you give an example of how to use a substitute for the humanoid please? As i personally feel like the humanoid is easier to use for that kinda thing. but i dont think the appearances are the issue i think its genuinely the loops and the delay because having kept all the npcs but without the loops it works fine…

humanoids are very heavy computationally, you can use animation controller instead to play animations, you can also use linear interpolation to move your npcs

local function Lerp(start, finish, percent)
    return start + (finish - start) * percent
end
local Animator = script.AnimationController.Animator -- your path to animator under AC
-- code
local animationTrack = Animator:LoadAnimation(script.Animation)
animationTrack:Play()

if you want to use physics, then use constraints, you simply set velocity by using math and apply it for X amount of time

I actually came up with a different way than having to switch it all up. I decided on “Loop throttling” so essentially the delay stays at 2.5 seconds on the loop until a player is nearby or a target is found in which it then goes to 0.1.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.