How would I make my AI "more smart"

Hello,
So I’m trying to make a game that has an AI like Piggy or Kitty. These AIs are very smart and have no problems climbing stairs, etc. But I’ve noticed that this is very hard to accomplish. I’m using the PathfindingService but I have had no luck. I set “AgentCanJump” off because I do not want my NPC to be able to jump, just like Piggy and Kitty. But if there is a small wall or table ( something the AI would normally jump over ) It just gets stuck. Even setting agentCanJump off.

Script:

local playersService = game:GetService('Players')
local pathService = game:GetService('PathfindingService')

local npc = script.Parent
local head = npc.Head
local hrp = npc.HumanoidRootPart
local humanoid = npc.Humanoid

npc.PrimaryPart:SetNetworkOwner(nil)

local npcPath = pathService:CreatePath({
	['AgentRadius'] = 5.7,
	['AgentHeight'] = 9.3,
	['AgentCanJump'] = false
})

local function requestTarget()
	
	if #playersService:GetPlayers() == 1 then
		return game.Players:GetPlayers()[1].Character
	elseif #playersService:GetPlayers() < 1 then
		playersService.PlayerAdded:Wait()
	end
	
	local chosenPlayer = playersService:GetPlayers()[math.random(1,#playersService:GetPlayers())]
	
	for _, player in pairs(playersService:GetPlayers()) do
		if player.Character and player.Character.Humanoid and player.Character.Humanoid.Health > 0 then
			local playerHead = player.Character.Head
			if (head.Position - playerHead.Position).Magnitude < (head.Position - chosenPlayer.Character.Head.Position).Magnitude then
				chosenPlayer = player
			end
		end
	end
	
	return chosenPlayer.Character
end

local function track(target)
	
	if not target or not target.Humanoid or target.Humanoid.Health == 0 then
		warn('Error, No target or no character, or target is dead')
		return
	end
	
	npcPath:ComputeAsync(hrp.Position,target.HumanoidRootPart.Position)
	
	if npcPath.Status == Enum.PathStatus.Success then
		
		for _, waypoint in pairs(npcPath:GetWaypoints()) do
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	else
		humanoid:MoveTo(hrp.Position - Vector3.new(1,0,0))
	end
end

local function main()
	
	local target = requestTarget()
	
	if target and target.Humanoid then
		if target.Humanoid.Health > 0 then
			track(target)
		end
	end
end

while wait() do
	main()
end

Thanks in advance!

1 Like

Why are you replying to my post if you haven’t got anything helpful to say?

1 Like

I would set jump to true because, in real life, things like this would be able to jump. AI that can jump over things IS smarter because the objective is to catch people and smart AI knows the fastest way to do that is jumping :smiley:

just if you find any solution for this problem to reply me :slight_smile: because is very annoying my customer jump in a structure i place… or if the customer is on floor 1 he go down by jumping from the floor and not use the stairs

5 Likes

The piggy AI doesn’t jump, and if I allow it to jump, it will jump all the time. The piggy AI is smart WITHOUT having CanJump on.

The NPC isn’t that bad TBH. The problem is that when I set CanJump to false, it doesn’t find a path that doesn’t involve jumping.

I need it so that it finds a path that it can get to WITHOUT jumping at all.

they’re not “very smart” they’re using the same path seeking that is shown on the devhub

Currently on mobile so apologies for the short reply.

Your solution lies with using Raycast in conjunction with PathfindingService. When an NPC gets stuck with pathfinding service, it fires a signal. When that signal is fired, you should see if your NPC is being blocked by something it can jump over (Ex, Raycast at Head and at HumanoidRoot.). Then go from there.

1 Like

That’s not true, a lot of developers stray away from the built in PathfindingService and make their own versions that better suit their games.

So will I still need to make the NPC jump though?

You have two options, regenerate the path until it generates one without a jump required, or use raycasting to detect a jump-able object and make your NPC jump

2 Likes

What do you mean by re-generate the path? How would I do that?

I tried that but it’s the same.

The piggy ai follows the players movements, if you move left it will move left at that same point. It only uses path finding if the player went through an obstacle they cant get past.

If you play piggy enough you will actually realise this. I think it saves the last few seconds of movement to a table then follows that and after its moved to that point it removes that position and adds a new position (the current position of that player its following) however if it isnt targeting anyone i think it does just use the path finding AI

Hope this helps!

Do you have any idea how I could implement this?