I've got a bot that chooses between hiding and chasing but it doesnt work correctly

so as i said i made this script and it works sort of the chase feature only runs once so even when the value is on the chase value the nextbot only goes to the first position and if the player moves the path doesnt recalculate

local ServerStorage = game:GetService(“ServerStorage”)
local SimplePath = require(ServerStorage:WaitForChild(“SimplePath”))

local Goal = nil
local Bot = script.Parent
local Path = SimplePath.new(Bot)

function findnearestPlayer(Origin:Vector3,MaxDistance:number)
local Players = game:GetService(“Players”)
local NearestPlayer,NearestDistance

for i,player in pairs(Players:GetPlayers()) do
	local character = player.Character
	local distance = player:DistanceFromCharacter(Origin)

	if not character or distance > MaxDistance or (NearestDistance and distance >= NearestDistance) then
		continue
	end


	NearestDistance = distance
	NearestPlayer = player
end

return NearestPlayer

end

function goal()
while game.ReplicatedStorage.Rand.Value <= 3 do
wait()
print(“Eaki”)
Goal = findnearestPlayer(Bot.PrimaryPart.Position,math.huge)
if Goal then
if Goal.Character.Humanoid.Health > 0 then
Path.Visualize = false
Path:Run(Goal.Character.PrimaryPart.Position)
Path.Blocked:Connect(function()
Path:Run(Goal.Character.PrimaryPart.Position)
end)
local humanoid = Goal.Character:WaitForChild(“Humanoid”)
if (Goal.Character.PrimaryPart.Position - Bot.PrimaryPart.Position).Magnitude < 8 then
–game.ReplicatedStorage.StringStorage.NexbotName.Value = Bot.Name
humanoid.Health = 0
script.Parent.HumanoidRootPart.KillSound:Play()
end
end
end
end
end

function Hide()
local bot_pos = Bot.PrimaryPart.Position;
local furthest_data = {nil, 0.0};
for _, part in next, workspace.ChoosablePaths:GetChildren() do
local dist = (part.Position - bot_pos).Magnitude;
if dist > furthest_data[2] then
furthest_data[1] = part;
furthest_data[2] = dist;
Path:Run(furthest_data[1].Position)
while game.ReplicatedStorage.Rand.Value <= 3 do
goal()
end
end
end
return furthest_data[1];
end

while true do
wait()
–local safezoneVal = game.ReplicatedStorage.BoolStorage.Safezone.InSafezone.Value
–val = game.ReplicatedStorage.BoolStorage.Safezone.InSafezone
–if safezoneVal == false then
if game.ReplicatedStorage.Rand.Value <= 3 then
while game.ReplicatedStorage.Rand.Value <= 3 do
goal()
end
elseif game.ReplicatedStorage.Rand.Value >= 4 then
local bot_pos = Bot.PrimaryPart.Position;

	local furthest_data = {nil, 0.0};
	for _, part in next, workspace.ChoosablePaths:GetChildren() do
		local dist = (part.Position - bot_pos).Magnitude;
		if dist > furthest_data[2] then
			furthest_data[1] = part;
			furthest_data[2] = dist;
			Path:Run(furthest_data[1].Position)
			script.Parent.Visible.OnServerEvent:Connect(function(plr:Player,NUL,onScreen)
				if game.ReplicatedStorage.Rand.Value <= 3 then
					while game.ReplicatedStorage.Rand.Value <= 3 do
						goal()
					end
				elseif game.ReplicatedStorage.Rand.Value >= 4 then
					Hide()
				end
			end)
		end
	end
	return furthest_data[1];
end

end