Help on making an ai that chooses between two values hiding and chasing

i already have it so it will choose two values and if it picks one it will go towards and one to go away but i want the running away one to actually hide behind a wall or something how would i do this heres the ais code 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

while true do
local rand = game.ReplicatedStorage.Ints.Choose.Value
Goal = findnearestPlayer(Bot.PrimaryPart.Position,math.huge)
if Goal then
if Goal.Character:WaitForChild(“Humanoid”).Health > 0 then
if rand == 1 then
Path.Visualize = true
Path:Run(Goal.Character.PrimaryPart.Position)
Path.Blocked:Connect(function()
Path:Run(Goal.Character.PrimaryPart.Position)
end)
else
if Goal then
if Goal.Character:WaitForChild(“Humanoid”).Health > 0 then
Path.Visualize = true
Path:Run(Goal.Character.PrimaryPart.Position - Goal.Character.PrimaryPart.Position - Goal.Character.PrimaryPart.Position)
Path.Blocked:Connect(function()
Path:Run(Goal.Character.PrimaryPart.Position - Goal.Character.PrimaryPart.Position - Goal.Character.PrimaryPart.Position)
end)
end
end
end
if (Goal.Character.PrimaryPart.Position - Bot.PrimaryPart.Position).Magnitude < 4 then
local Humanoid = Goal.Character:FindFirstChildOfClass(“bot”)
if Humanoid then
Humanoid:TakeDamage(0) – keep as 0
end
end
end
end
task.wait()
end

I am not quite sure what you mean exactly, but you can jut use

if math.random(2) == 2 then -- Hiding
   ... -- Hiding script
else
   ... -- Chasing script
end

Like be more specific, when do you want it to end, when do you want it to pick etc.

i already have the switching mechanic and it just picks a new value every handful of secons like this while wait() do
local rand = game.ReplicatedStorage.Ints.Choose
wait(math.random(1,2))
rand.Value = math.random(1,2)
end

what im struggling with is the hiding part like where does it go where will it hide how to figure out where it needs to hide

Alright so to hide you want it to run directly away from the player. Normally for this you’d need to put parts around and then pathFind to these parts. You want to try and get the furthest part from the player and make the NPC move to that part without crossing paths with the player.

1 Like

i’ll try but i would like to just have it find that are without parts since their are going to be more than one map and thats a lot of placing

You just need like 5 for a pretty big map. Like in the main rooms.

so i got it working sort of it goes to the part if its dist is more than 45 but it doesnt go to the farthest and if it chooses two again it freaks out because it tries to go to another far part but then chooses to go back to the one it was already on, what math would i do to find the farthest one because right now im using
for _, path in pairs(game.Workspace.ChooseablePaths:GetChildren()) do
local dist = Vector3.new(Goal.Character.HumanoidRootPart.Position.X + path.Position.X, Goal.Character.HumanoidRootPart.Position.Y + path.Position.Y, Goal.Character.HumanoidRootPart.Position.Z + path.Position.Z).Magnitude
if dist > 45 then
local savedPath = path
Path:Run(savedPath.Position)
end
end

local function getFurthestFromPlayer(botPos, plrPos)
   local furthest, relative = nil, 0
   for _, path in pairs(game.Workspace.ChooseablePaths:GetChildren()) do
      local pos = path.Position
      local d1, d2 = (botPos - pos).magnitude, (plrPos - pos).magnitude
      local r = d1/d2 -- relative distance
      if not furthest or relative >= r then
         furthest = path
         relative = r
      end
   end
   return furthest
end

heres the modified script i cant use the local function but it doesnt work it does however print the right distance

					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

nvm i got it working but i am confused it does only do it once so its not getting new values each second but its still not going to the farthest one

your original way worked i just had to wrap both my chase and hide scripts in local functions thanks :slight_smile:

it does work now but once it chooses a part it goes that part and when it goes after the player then goes back to hiding it goes to the same part even though now its not the farthest part???