Still have questions? Join our Discord server and get real time help.
0

# I made this following code and i want to know if there is some way to make it more efficient?

caipira 29
2 years ago

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

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. M39a9am3R 3209 — 2y
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 caipira 29 — 2y