I’ve been working on a monster AI and I’ve been wondering how I can make it retreat when it takes too much damage. Anyone know how I can go about it?
local hum = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")
local PathfindingService = game:GetService("PathfindingService")
local Retreat = workspace.RetreatPoint
local function GetPath(destination)
local path = PathfindingService:CreatePath()
path:ComputeAsync(torso.Position, destination)
return path
end
local function move(destination)
local path = GetPath(destination)
for index, waypoint in pairs(path:GetWaypoints()) do
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
end
local function patrol()
local Points = workspace.Points:GetChildren()
local RandomPoint = math.random(1, #Points)
move(Points[RandomPoint].Position)
end
while wait(0.5) do
patrol()
end
local hum:Humanoid? = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")
local PathfindingService = game:GetService("PathfindingService")
local Retreat = workspace.RetreatPoint
function Calculate14(health:number?):number?
return (hum.Health / 4)
end
local function GetPath(destination)
local path = PathfindingService:CreatePath()
path:ComputeAsync(torso.Position, destination)
return path
end
function RetreatBack()
hum:MoveTo(Retreat.Position)
end
local function move(destination)
local path = GetPath(destination)
for index, waypoint in path:GetWaypoints() do
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
if hum.Health <= Calculate14() then
task.spawn(RetreatBack)
break
end
end
end
local function patrol()
local Points = workspace.Points:GetChildren()
local RandomPoint = math.random(1, #Points)
move(Points[RandomPoint].Position)
end
while task.wait(0.5) do
if hum.Health >= Calculate14() then
patrol()
end
end