Npc System: Usage / FAQ's

Create an Npc object

  1. To create an Npc object you will first need a character. I will be using this pirate character from the catalog.

  2. Then, create a new script in ServerScriptService. This is will be where we will be writing the code for our Npc.

  3. Drag the character model into the script.

  4. Now, we can start writing our code. Open up the script and create a variable for the Npc Module located in ServerStorage, and also a variable for the character model we put in the script.

  5. Then we can create a variable for our Npc object.

  6. Now we will edit the Npc object’s properties, this is identical to regular roblox instances. so don’t get scared. Since my Npc is a pirate, I will create a pirate team, and set the Npc’s Team property to that team.

  7. Next, we can give our Npc some functionality, we can do this by calling methods on the Npc. The Npc system has various methods, but we will only be using the NavigateTo method for simplicity. We will be making the Npc walk to random Vector3 positions in a while true loop (a forever loop)

  8. Thats all! this is how my pirate Npc turned out. I think he’s a bit lost.

FAQ’s

"How do I set a custom animation pack for my Npc?"

To set a custom animation pack:

  1. Copy the R15 Animate script from Npc.Resources (or R6 if your animation is R6)

  2. Paste the Animate script into the character you’re using for your Npc, and delete the previous Animate script if there was one there.

  3. Select the Animate script, and paste the below code into your command bar, then run it. (this removes the default animations):

local currentSelection = game.Selection:Get()[1]

local namesOfChildrenToDelete = {
	"climb", "fall", "idle", "jump", "pose", "run", "swim", "swimidle", "walk",
}

if currentSelection.Name == "Animate" then
	for _, child in currentSelection:GetChildren() do
		if not child:IsA("StringValue") then continue end
		if table.find(namesOfChildrenToDelete, child.Name) then child:Destroy() end
	end
end
  1. Insert your desired animation pack. (often with a plugin) I inserted the Roblox Pirate Animation Pack from the catalog

  2. Parent the inserted animations to the Animate script.
    NpcSystemAnimateFAQGIF

"How do I add methods to the Npc Class?"

Adding methods are relatively easy, for this example I will be making an ‘Npc:Explode()’ method.

  1. First, open the Npc module and find some space to create the new function. I will be adding mine just under the Fling method.

  2. Now, we write the code for our function. The code in my Explode function parents an explosion instance to the Npc’s character, which will make the character explode.

  3. We could leave our method like this without any parameters, but im gonna add an optional ‘blastForce’ parameter, just to make this method a little more interesting.

  4. Now that were done creating our method, we must playtest! this is how my explode method turned out.

Note: We use the ‘self’ keyword inside methods instead of Npc, they are essentially identical, though.

"How do I add properties to my Npc Class?"

To add custom properties:

  1. First, declare the property in the Npc class constructer, and its default value. I will be making a ‘GodMode’ property, which will be false by default.

  2. Then, declare the new property’s type. my ‘GodMode’ property is a boolean (true or false)

  3. Give the property functionality. I want my property to give the Npc infinite health when set to true, and back to 100 when set to false. We will do this by creating new branches in the index (getter) and newindex (setter) metamethods.


  4. index (getter) When a user tries to index (get) the ‘GodMode’ property, (e.g. print(someNpc.GodMode)) it will return whether the Npc has infinite health or not.

  5. newindex (setter) When a user tries to newindex (set) the ‘GodMode’ property, (e.g. someNpc.GodMode = true) if the setting value is true, it will give the Npc infinite health, if the value is false, it will set the Npc’s health back to 100.

  6. This is how it turned out! it sets the Npc’s humanoid’s health to inf.