I made this code to make a enemy follow you when you get near, but it keeps calculating the distance without stopping, and i was wondering if there is a some way to do it run only when the player is close

local player = game.Players.LocalPlayer
local enemy = workspace.enemy


while true do 
	local dist = (workspace:WaitForChild(player.Name):WaitForChild("HumanoidRootPart").Position - enemy.HumanoidRootPart.Position).magnitude
	wait(.5)
	if dist < 30 then
		enemy.Humanoid.WalkToPoint = workspace:WaitForChild(player.Name).HumanoidRootPart.Position
		
	end
end

2 comments

1

I would say if the player is a farther position away than what the bot needs, then have the loop on a longer wait time. The closer the bot, the quicker the wait time goes. Unfortunately with magnitude, there is not much to do to increase performance which I know of.

0

actually i did it in the real code, i just wanted to know if there was a way to increase perfomance without doing these calculations, but thanks :D