Make a Smart NPC

I wan’t to make a gamemode in my game (Capture The Flag) and this gamemode is for 2 players, well i want to make this for 1 player, the other player is an NPC, well, i have some problems, i want to the NPC walks to the flag in a path, not straight, and later that he comes back and if he finds a player he do something. Could you help me? Thanks :+1:

3 Likes

You can use pathfinding.

local PathfindingService = game:GetService("PathfindingService")

local npc = game.Workspace.NPC
local humanoid = npc.Humanoid
local destination = game.Workspace.RedTeamsFlag

local path = PathfindingService:CreatePath()

path:ComputeAsync(npc.HumanoidRootPart.Position, destination.PrimaryPart.Position)
1 Like

I can keep the script inside of the NPC and say that the npc is the parent?

Yes, you can. (30 characters…)

So what we’re talking about here is an NPC that can perform different actions, depending on different situations.

Lets break these down briefly into some States;

  1. Searching for the flag
  2. Capturing the flag
  3. Attacking the player
  4. Chasing the player
  5. Chasing the player (With flag)
  6. Dead

Ok, so now we’ve defined a pretty limited amount of States our NPC can be in at any one time. This sort of layout lends itself to the use of “StateMachines”. Theres actually a pretty nice version of this system in use in the model: https://www.roblox.com/library/187790284/Soldier by @UristMcSparks
But for a more in-depth explanation of the idea, heres a step by step look at making and using one in Unity
https://www.youtube.com/watch?v=YdERlPfwUb0
Okay, so using this, we could make a pretty simple NPC State Machine, while this won’t be the most powerful thing, you will be able to do a lot with it!
Heres what I managed to make using nearly the same state machine as inside that model;
Brutal (Alpha) - Roblox

2 Likes

Well, does Unity use LUA? I would like to try Unity but my laptop has the power of an average phone.

I set this in a blank R6 character from Moon animator, and this is not working:

 

local PathfindingService = game:GetService("PathfindingService")
    wait(10) 
    
    local zombie = script.Parent
    local humanoid = zombie.Humanoid
    local destination = game.Workspace.easnr

    local path = PathfindingService:CreatePath()
    
    path:ComputeAsync(zombie.HumanoidRootPart.Position, destination.PrimaryPart.Position)

Do you get any errors in output?

Also make sure that:

  1. The character has a HumanoidRootPart.
  2. The destination is a model with a “Primary part”. Change it through properties of the model

I don’t have any error

Yes it has, Moon animator’s loading dummy will always have a HumanoidRootPart, since that is required to animate.

That was my first error but i already fixed it and there’s not any change.

I made this uncopylocked place:

Feel free to use it for your game!
Good luck!

2 Likes