Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
caipira 29
6 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 3210 — 6y
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 — 6y

Answer this question