PathFinding AI acting up

I’ve created a pathfinding script where my custom rig will wander around from part to part until a humanoid comes close enough. i’ve created custom animations, too. Unfortunately, my rig just zooms around in circles while playing no animations and not going to the correct part or attacking the player.

Edit: this same script has worked for other models, just not this one

All help is appreciated!

Update: I’ve fixed him zooming in circle by changing while wait loop from 0.5, to 5. So, he now attempts to go to a waypoint, but continues through the waypoint and zooms off the map

He still plays no animations and his walkspeed is set to 1, but he still zooms around like usain bolt

If you show us the script, we can see what might be going wrong. Otherwise we will be guessing

Hello there,

my apologies for the late response, ive been busy lately and didn’t see you’re reply until now. So, i have fixed the AI by adjusting the humanoid hipheight. i do have a problem with his animation not playing though.

Here is the pathfinding script:

local spider = script.Parent
local humanoid = spider.Humanoid

local PFS = game:GetService("PathfindingService")

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 100
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (spider.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	return nearestTarget
end

local function getPath(destination)
	
	local pathParams = {
		["AgentHeight"] = 15,
		["AgentRadius"] = 2,
		["AgentCanJump"] = false
	}
	
local path = PFS:CreatePath(pathParams)

	path:ComputeAsync(spider.HumanoidRootPart.Position, destination.Position)
	
return path
end

local function attack(target)
	local distance = (spider.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	
	if distance > 15 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
		spider.Humanoid.WalkSpeed = 16
	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("Found Target, YUMMY YUMMY!"..target.Name) attack(target)
			break
		else
			humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
				humanoid.WalkSpeed = 16
		end
		end
	else
		humanoid:MoveTo(destination.Position - spider.HumanoidRootPart.CFrame.LookVector * 10)
	end
			end

local function Wander()
	local waypoints = game.Workspace.NPCPaths.SpiderPath1:GetChildren()
	local randomNumber = math.random(1, #waypoints)
	walkTo(waypoints[randomNumber])
end

while wait(0.5) do
Wander()
	end

and i’m using the default roblox animation script to animate the rig

I expect you need to determine if the statement if path.Status == Enum.PathStatus.Success then is ever valid. Add a few print statements to determine which MoveTo is being processed. From your initial statements, I would guess that it only ever runs this:

humanoid:MoveTo(destination.Position - spider.HumanoidRootPart.CFrame.LookVector * 10)

If it does, then you could also add a MoveToFinished:Wait()

1 Like

I’ll test it out later, thanks!

Hey, i used your suggestions and it doesnt help, i fear its something to do with the rigging of my model. Thanks a ton though