Why does my flee script not work?

What do you want to achieve? A well working flee system for an AI

What is the issue? The AI can’t find a suitable place to flee to.

What solutions have you tried so far? I’ve edited the code so many times and have used many methods but none of them have worked well.

local function findThreat()
	local dist = 175
	local threat = nil
	local function search(v)
		local tool = v:FindFirstChildOfClass("Tool")
		if tool then
			if tool.Name == "Push" then
				dist = 15
			elseif tool.Name == "NextbotTool" then
				dist = 300
			end
			if v:IsA("Model") and v.PrimaryPart then
				if (v.PrimaryPart.Position - botRoot.Position).Magnitude <= dist then
					if threat ~= nil and v.PrimaryPart then
						if (botRoot.Position - v.PrimaryPart.Position).Magnitude < (botRoot.Position - threat.PrimaryPart.Position).Magnitude then
							threat = v.HumanoidRootPart
							warn("Found threat!")
						end
					elseif v.PrimaryPart then
						threat = v.PrimaryPart
					end
				end
			end
			dist = 175
		end
	end
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == game.Teams.Runners and v.Character then
			search(v.Character)
		end
	end
	for i,v in pairs(workspace.AIs:GetChildren()) do
		search(v)
	end
	for i,v in pairs(workspace.Nextbots:GetChildren()) do
		search(v)
	end
	
	return threat
end

local function flee()
	local threat = findThreat()
	if threat then
		warn("Fleeing!")
		local goal = Vector3.zero
		
		local dist = (script.Parent.HumanoidRootPart.Position-threat.Position).Magnitude
		
		local goal = Vector3.zero
		
		local furthest = 0
		
		local success = 0
		
		repeat
			local pos = script.Parent.HumanoidRootPart.Position
			local jittered = pos+Vector3.new(
				math.random(-5,5),
				0,
				math.random(-5,5)
			)
			if (threat.Position-jittered).Magnitude > dist then
				local ray = Ray.new(pos,jittered)
				local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{workspace.AIs,workspace.Nextbots,script.Parent})
				if not hit then
					if isPointOnLand(jittered) and (threat.Position-jittered).Magnitude > furthest then
						furthest = (threat.Position-jittered).Magnitude
						goal = jittered
						success+=1
					elseif not isPointOnLand(jittered) then
						warn("Not on land!")
					end
				else
					warn("Stuck!")
				end
			end
			wait()
		until success >= 3
		
		if goal ~= Vector3.zero then
			botNoid:MoveTo(goal,true)
			EndMoveTick()
			warn("Proper Dodge System")
		else
			--[[local cframe = botRoot.CFrame * CFrame.new((threat.Position - botRoot.Position).Unit * 50)
			botNoid:MoveTo(cframe.Position) -- old, fashioned unit method
			EndMoveTick()
			warn("Alternative Dodge System")]]
		end
	else
		warn("No threat!")
		return nil
	end
end