Buggy Custom AI, how can i fix it?

I am developing a game that plays like piggy, and I’ve coded a custom bot, but it gets stuck all the time, on walls, it can’t go down certain hallways that are certainly wide enough for it, and it is altogether just quite buggy. I’ve checked where it gets stuck, and it should be able to pathfind through there, but instead it just does what it does when a paths success is false

– createPath function

local function getPath(target)
	local pathParams = {
		["AgentHight"] = 9,
		["AgentRadius"] = 3.5,
		["AgentCanJump"] = true
	}
	local path = pathfindingservice:CreatePath(pathParams)

	path:ComputeAsync(sunny.HumanoidRootPart.Position, target.HumanoidRootPart.Position)

	return path
end

– walkTo function

local function walkTo(target)
	local path = getPath(target)
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			if target and target.Humanoid.Health > 0 then
				if CanSeeTarget(target) then
					humanoid:MoveTo(target.HumanoidRootPart.Position)
					sunny.Eyes.Color = sunny.GlowingEye.Color
					sunny.Eyes.Material = Enum.Material.Neon
					break
				end
				
				humanoid:MoveTo(waypoint.Position)
				sunny.Eyes.Material = eyeMaterial
				sunny.Eyes.Color = eyeColour
				humanoid.MoveToFinished:Wait()
			else
				sunny.Eyes.Material = eyeMaterial
				sunny.Eyes.Color = eyeColour
				humanoid:MoveTo(sunny.HumanoidRootPart.Position - Vector3.new(math.random(-5,5), 0, math.random(-5,5)))
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo((sunny.HumanoidRootPart.Position - Vector3.new(0, 0, -3)) - (sunny.HumanoidRootPart.CFrame.LookVector * 2))
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		wait(0.1)
	end
end	
local function patrol()
	local target = findTarget()
	if target then
		walkTo(target)
		target = findTarget()
	end
end
while wait(0.1) do
	patrol()
end
1 Like

may i see your piggy model properties? i have this issue the same as you.

Alright, just a minute :smiley:

image

image

it’s bounding box is around 7 studs across

wait… Did you destroy a head?

Sorry, I’m not sure what you mean?

Correct me if im wrong, you are using a R15 man RIG, which has a default head in it. If the humanoid doesnt have a head, it will not count as a humanoid

No, I renamed the neck part to Head, and the black circle on the actual head as realHead, just to make animating simpler

But the rig works perfectly, it’s the code itself that is buggy

Is it R6 or R15 or you forgot to unanchor the rig

No, as I said, the rig works fine, the bot works for the most of the time, but it gets stuck and can’t go down certain empty hallways, and that’s what is confusing me. But thank you anyways :smiley:

while wait(0.1) do
patrol()
end

if you looping it, the function will add to a queue. Try not using this loops

and not sure why you need to make a path sucess to false
As a logic: example that they was a player on a floating part
So the path wont be sucess, If it didnt even sucess and why would you try moving then

Think I fixed it for now at least, i just turned the AgentRadius down and made the bot’s collision smaller