Hello, I need some help figuring out why my AI is confused. My script looks for the nearest player and if there is a nearest player the AI will chase them, if not my AI gets confused. Out of nowhere, my AI randomly broke in-game and in studio, I have done some debugging and noticed that there are no waypoints. AI’s behavior:
robloxapp-20240514-1350543.wmv
I have been trying to find the solution to this, but nothing works.
Here is my script:
while humanoid.Health > 0 and workspace:FindFirstChild("TestingPlace") do
task.wait(math.min((60-workspace:GetRealPhysicsFPS())/59,5))
local success, errorMsg = pcall(function()
local players = {}
for _, char in pairs(workspace:GetChildren()) do
if char:FindFirstChild("Humanoid") and char ~= bot then
table.insert(players,char)
end
end
local target = nil
for _,char in pairs(players) do
local mag1 = (botRoot.Position - char.HumanoidRootPart.Position).Magnitude
local mag2 = (botRoot.Position - target.HumanoidRootPart.Position).Magnitude
if typeof(target) == "nil" then
target = char
elseif mag1 <= mag2 then
target = char
end
end
end
if target then
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(botRoot.Position,target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if path.Status==Enum.PathStatus.Success then
local specificWaypoint = waypoints[math.clamp(3,1,#waypoints)]
if specificWaypoint then
local waitTime = (specificWaypoint.Position-botRoot.Position).Magnitude/(bot.Humanoid.WalkSpeed/.3)
bot.Humanoid.MoveTo(specificWaypoint.Position)
if specificWaypoint.Action == Enum.PathWaypointAction.Jump then
bot.Humanoid.Jump = true
end
task.wait(waitTime)
end
else
local position=botRoot.Position
local randomVector = Vector3.new(math.random(position.X - 5,position.X + 5),position.Y,math.random(position.Z - 5,position.Z + 5))
bot.Humanoid:MoveTo(randomVector)
task.wait(1)
end
else
local position=botRoot.Position
local randomVector = Vector3.new(math.random(position.X - 5,position.X + 5),position.Y,math.random(position.Z - 5,position.Z + 5))
bot.Humanoid:MoveTo(randomVector)
task.wait(1)
end
end)
if errorMsg then
warn(string.format("bot ai (%s): %s",bot.Name,errorMsg))
end
end
(Sorry for messy code)
I really need help with this, I don’t know how my AI broke, navmesh is fine though.