-
What do you want to achieve?
I’m trying to fix my AI so it stops getting stuck -
What is the issue?
The AI is getting stuck or something on the ground
-
What solutions have you tried so far?
I tried finding the solution on this website, but all of the things I checked didn’t work. I also tried changing agent height and radius, but that also didn’t work.
The AI script:
local TeddyAI = script.Parent
local chasing = false
local followingTarget = nil
local SimplePath = require(game.ServerStorage.SimplePath)
local path = nil
local reached = false
local reached2 = false
local hipHeight = script.Parent.Humanoid.HipHeight
local pathParams = {
["AgentHeight"] = 10,
["AgentRadius"] = 5.5,
["AgentCanJump"] = false
}
script.Parent.HumanoidRootPart:SetNetworkOwner(nil)
local function getHumPos()
return (TeddyAI.HumanoidRootPart.Position - Vector3.new(0,hipHeight,0))
end
local function displayPath(waypoints)
local color = BrickColor.Random()
for index, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.BrickColor = color
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(1,1,1)
part.Position = waypoint.Position
part.Parent = workspace
local Debris = game:GetService("Debris")
Debris:AddItem(part, 6)
end
end
local function findPotentialTarget()
local players = game.Players:GetPlayers()
local maxDistance = 100
local nearestTarget
for index, player in pairs(players) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
if player.Character.Humanoid.Health > 0 then
local target = player.Character
local distance = (TeddyAI.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
nearestTarget = player
maxDistance = distance
end
end
end
end
return nearestTarget
end
local function canSeeTarget(target)
if target and target:FindFirstChild("HumanoidRootPart") then
local origin = TeddyAI.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - TeddyAI.HumanoidRootPart.Position).unit * 10000
local ray = Ray.new(origin, direction)
local ignoreList = {TeddyAI}
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
-- check if it exists
if hit then
-- check if it hit
if hit:IsDescendantOf(target) then
-- check health
if target.Humanoid.Health > 0 then
-- check if target is safe or not
if not game.Players:GetPlayerFromCharacter(target).Safe.Value then
-- check if monster can see
local unit = (target.HumanoidRootPart.Position - getHumPos()).Unit
local lv = TeddyAI.HumanoidRootPart.CFrame.LookVector
local dp = unit:Dot(lv)
if dp > 0 then
return true
end
end
end
end
else
return false
end
end
end
local function getPath(destination)
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(getHumPos(), destination.Position)
return path
end
local function blockToBlock()
if path and not reached then
coroutine.wrap(function()
path.Reached:Wait()
reached2 = true
end)()
repeat wait() if path == nil then break end until reached2 == true
end
reached = false
reached2 = false
path = nil
coroutine.wrap(function()
wait(1)
if followingTarget and chasing == false then
-- disable stuff for all targets
for i, v in pairs(game.Players:GetPlayers()) do
if v.GettingChasedBy.Value == script.Parent then
v.GettingChased.Value = false
v.GettingChasedBy.Value = nil
end
end
TeddyAI.Chasing.Value = false
followingTarget = nil
end
end)()
local goal = workspace.LoopPoints:GetChildren()[Random.new():NextInteger(1,#workspace.LoopPoints:GetChildren())]
local path = getPath(goal)
if path.Status == Enum.PathStatus.Success then
--displayPath(path:GetWaypoints())
for i, v in pairs(path:GetWaypoints()) do
if findPotentialTarget() then
if canSeeTarget(findPotentialTarget().Character) then
break
end
end
TeddyAI.Humanoid:MoveTo(v.Position)
local check = TeddyAI.Humanoid.MoveToFinished:Wait(1)
if not check then
blockToBlock()
break
end
end
else
end
end
TeddyAI.Chasing.Changed:Connect(function()
end)
script.Parent.Teleport.Event:Connect(function()
path = nil
end)
while true do
local target = findPotentialTarget()
if target and canSeeTarget(target.Character) and target.Character.Humanoid.Health > 0 then
path = SimplePath.new(script.Parent,pathParams)
--path.Visualize = true
local connection = path.Reached:Connect(function()
reached = true
script.Parent.Reached:Fire(target)
end)
repeat
chasing = true
followingTarget = target
-- enable stuff for target
target.GettingChased.Value = true
target.GettingChasedBy.Value = script.Parent
TeddyAI.Chasing.Value = true
path:Run(target.Character.HumanoidRootPart.Position)
until not path or path.LastError == "ComputationError" or not target.Character or target.Character:FindFirstChild("HumanoidRootPart") == nil or target.Character.Humanoid.Health < 1 or target.Safe.Value or findPotentialTarget() ~= target or reached
if connection then
connection:Disconnect()
end
if findPotentialTarget() ~= target then
if path and path._moveConnection then
path:Stop()
end
end
chasing = false
else
blockToBlock()
end
game:GetService("RunService").Heartbeat:Wait()
end
Update: Found out it’s something to do with changing ground type (grass, pavement, rock, etc.)