Hello,
So I’ve been using the Teddy AI pathfinding script from the youtuber GnomeCode and have been running into an issue where it gets stuck or even bumps into walls while chasing you in my maze. I’ve tried getting a response from them but had no luck, tried looking for threads relating to this issue but didn’t find anything useful for myself. I have no clue what to do to say the least as I don’t specialize in scripting nor know very well how the pathfinding system works by heart.
Video of what’s happening:
As you see, it does chase you but gets stuck on walls when you are walking next to them.
How things are set up (monster is in Workspace):
Code inside the “AI” script:
local Igor = script.Parent
local humanoid = Igor.Humanoid
Igor.PrimaryPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
local origin = Igor.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - Igor.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, Igor)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 60
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Igor.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local PathfindingService = game:GetService("PathfindingService")
local pathParams = {
["AgentHeight"] = 8.25,
["AgentRadius"] = 2.85,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(Igor.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Igor.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 2 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local attackAnim = humanoid:LoadAnimation(script.Attack)
attackAnim:Play()
target.Humanoid.Health = 0
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
print("TARGET FOUND", target.Name)
attack(target)
break
else
print("Moving to ", waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (Igor.HumanoidRootPart.CFrame.LookVector * 10))
end
end
function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait() do
patrol()
end
This has been a problem for the longest time ever since I implemented it in and have no idea how to fix this, I’d truly appreciate anyone who could be kind enough to assist me with this problem I’m facing! Somehow doesn’t give me problems in the testing game but once in put it in my main, then that’s where the problem starts.