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 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
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.
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
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???