Will making an enemy move to the player with this method cause a lot of lag?

I have a script that checks if the player is in the enemy’s field of view, and is close enough to attack. If I create 50 of these enemies in a game, will this cause lag?
Here is the code :

local rs = game:GetService("RunService") 
local char = game.Players.LocalPlayer.Character
local enemy = game.Workspace.Enemy
rs.RenderStepped:Connect(function()
	local partcharacter = (char.Head.Position - enemy.Head.Position).Unit
	local enemylook = enemy.Head.CFrame.LookVector
	local dot = partcharacter:Dot(enemylook)
	local y = char:WaitForChild("UpperTorso").Position - enemy.Head.Position
	local dist = y.magnitude
	if dot > 0.5 and dist < 25 then
		while wait() do 
            print(dist)
			enemy.Humanoid:MoveTo(char.HumanoidRootPart.Position)
		end 
	end
end)

`

I also ran into an error where for the if statement, both conditions are met and the script enters the while loop and it starts printing the variable that I named dist but the enemy doesn’t move and i’m not sure how to fix it, though this only happens the first time I try going close to it but, it fixes itself once I almost get close enough to touch it, even though I specified for it to be 25 magnitude away.

If this script is running on the server then you already have two illegal access points: trying to index LocalPlayer (it’s nil) and RenderStepped (client-only). The script won’t work to begin with.

If you’re using this from the client then yes this will be relatively non-performant. You should never be using RenderStepped unless you’re updating something inexpensive (e.g. camera’s CFrame) so use Heartbeat instead. Expensive computations in RenderStepped will hinder a client’s ability to render frames since code in RenderStepped needs to finish first.

The other problem is that you have a non-terminating while loop (improperly used - wait should not be used as the condition of your while loop) that will get spawned up every time RenderStepped is fired and the conditionals are met. That’s grounds for spawning a while loop every frame prior to rendering. Multiply this by the number of times you’re inserting this script since you’re not using one script to control all the NPCs and you have a bunch of while threads permanently executing.

You don’t even need the while loop since the RunService event would handle updates every frame as to whether or not the NPC can see the player and the new position it should move to for the frame.

2 Likes

So basically I should remove the while loop and use heartbeat instead of renderstepped? K thanks I will try it but will it still be performing well when there’s maybe 40-50 of these(I tried it for one enemy and my computer’s cpu usage spikes, I’m not sure how much worse that will get when theres multiple), or is there another way to do it or improve this further? Also do u know how to fix my other issue? I’m not sure why but even though I set the distance to be less than 25 I have to get close enough for the magnitude to be 5 for it to come towards me, but only the first time around.

Also instead of using RunService would it work if I just used a while loop and had a wait inside for 0.1 seconds?