How to make an ai that chooses between hiding and chasing

I already have the base that chooses between those two and calculates the farthest one but it only calculates it once, and once it calculates it even if it’s no longer the farthest part it still goes there how would I fix this

``````			local furthest, relative = nil, 0
for _, path in pairs(game.Workspace.ChooseablePaths:GetChildren()) do
local pos = path.Position
local d1, d2 = (Bot.PrimaryPart.Position - pos).magnitude, (Goal.Character.PrimaryPart.Position - pos).magnitude
local r = d1/d2 -- relative distance
if not furthest or relative >= r then
furthest = path
relative = r
print(furthest.Position)
while rand == 2 do
Path:Run(furthest.Position)
end
end
end
return furthest
``````

I have already tried wrapping it in while true do and turning it into a function and calling it in while true do but it still only calculates it once

I dont understand what are you trying to do here exactly.

I suppose `game.Workspace.ChooseablePaths` its a folder with parts. Then

On a loop iterating those parts, you create 2 variables reading the distance.
`d1`, contains the distance from `Bot.PrimaryPart` to the path selected by the loop (lets say 5 studs)
`d2`, contains the distance from `Goal.Character` to the path selected by the loop (lets say 10)

Then you divide 5/10, getting 0.5

If not `furthest` (the first time the loop iterated, furthest is nil) OR if `relative` is greater or equal to 0.5 (you will never get `relative` greater than `r`, no matter what distances you divide `r = d1/d2` those will always be greater than 0)
Anyway, so `furthest` is nil, then you set `furthest` to contain the Instance `path`, `relative` variable to `0.5`, prints the first `path.Position` you found in the loop, and start a `while` loop, what is `Path:Run()`? you are not showing it, whats `rand`?
Why dividing distance from Bot to path with distance from a Character to path?

As Im saying that code will run the while loop calling `Path:Run()` since the first iteration, doing not comparision about distances at all. Probably not running again due to the while loop calling `Path:Run()`

Sorry I was asleep

Rand = math.random(1,2) – I’m running this in a separate script that just does while wait math.random(5,15) do then inside the while wait do I just make it choose a random value between 1 or 2 and path:run() is simple path and I don’t know how to explain it but you need the module of simple path for it to work but you could just use Humanoid:MoveTo()