Npc System: Npc Class Documention

NpcSystemIcon_Mini Npc
An OOP class for Non-Player Characters.

Get the Npc class here: create.roblox.com/store/asset/80973226380705/Npc-System


Summary

Properties:

properties_icon Character : Model

properties_icon Name : string

properties_icon Health : number

properties_icon Backpack : Folder

properties_icon Team : Team

properties_icon RespawnOnDeath : boolean

properties_icon RespawnTime : number

properties_icon RegenerateHealth : boolean

properties_icon PickupToolsOnTouch : boolean

Methods:

methods_icon NavigateTo( location: Vector3, pathfind: boolean, agentParameters: Dictionary ) : ( )

methods_icon CancelNavigation ( ) : ( )

methods_icon Chat( message: string ) : ( )

methods_icon SetFace( imageId: string, duration: number ) : ( )

methods_icon Dance( danceNum: number ) : ( )

methods_icon Fling ( ) : ( )

methods_icon EquipTool( tool: Tool ) : ( )

methods_icon UnequipTools ( ) : ( )

methods_icon ActivateTool ( ) : ( )

methods_icon Freeze ( ) : ( )

methods_icon UnFreeze ( ) : ( )

methods_icon Ragdoll( duration: number, pushMagnitude: number) : ( )

methods_icon Unragdoll ( ) : ( )

methods_icon EnableShiftLock( orientation: Vector3, lookAt: Vector3 ) : ( )

methods_icon DisableShiftLock ( ) : ( )

methods_icon SetDialog( dialog: Dialog ) : ( )

methods_icon Respawn( location: Vector3 ) : ( )

methods_icon Clone ( ) : ( )

methods_icon Destroy ( ) : ( )

methods_icon Jump ( ) : ( )

methods_icon TakeDamage( amount: number ) : ( )

methods_icon LoadAnimation( animation: animation ) : ( AnimationTrack )

methods_icon AddAccessory( accessory: Instance ) : ( )

Events:

events_icon Spawned( ) : Signal

events_icon CharacterRemoving( ) : Signal

events_icon NavigateFinished( reached: boolean ) : Signal

Code Samples:

Basic Usage

Example code of an Npc named ‘John Doe’ who will dance 10 seconds after spawning.

local NpcClass = require(game.ServerStorage.Npc) -- require the Npc module

local npc = NpcClass.new() -- create the npc and set it's properties
npc.Name = "John Doe"
npc.Health = 100

task.wait(10)
npc:Dance() -- make the npc dance 🕺🕺

NpcClass.new

To create an Npc object you will have to use the new function on the NpcClass.
you will have to pass in a character parameter, this can either be:

  • A character model – Will use the character model
  • A user’s username/ID – Will use the user’s avatar
  • Blank – Will use the default character (Mr. Robot)
local NpcClass = require(game.ServerStorage.Npc)

local someCharacterModel = script.SomeCharacterModel
local npc1 = NpcClass.new(someCharacterModel) -- SomeCharacterModel
local npc2 = NpcClass.new("Builderman") -- Builderman's avatar
local npc3 = NpcClass.new() -- Mr. Robot

API Reference

Properties:

properties_icon Character

A reference to the Npc’s character model.

Npc.Character : Model

properties_icon Name

The name of the Npc. This is identical to Npc.Character.Name

Npc.Name : string

properties_icon Health

The amount of health the Npc’s humanoid currently has. This is identical to Humanoid.Health

Npc.Health : number

properties_icon Backpack

The container used for tools that aren’t equipped. This is similar to Roblox Player Backpacks

Tool(s) that are equipped when UnequipTools() is called go to the backpack.

Npc.Backpack : Folder

properties_icon Team

The Roblox Team the Npc belongs to. This is similar to Player.Team.

Npc’s will respawn at SpawnLocation’s matching their Team’s TeamColor.

Note that if the Npc touches a SpawnLocation that has AllowTeamChangeOnTouch enabled, the Npc’s team will change to the team matching the SpawnLocation’s TeamColor.

Npc.Team : Team

properties_icon RespawnOnDeath

Whether the Npc respawns when they die.

On death, (when the Npc’s Health reaches 0) the Npc will go to a dead state for [RespawnTime] amount of seconds, and will respawn if RespawnOnDeath = true, otherwise, the Npc will stay in a death state forever. You will have to manage cleanup of dead Npc’s manually.

Npc.RespawnOnDeath : boolean

properties_icon RespawnTime

How long the Npc stays in a dead state before they respawn. This property will not have affect if RespawnOnDeath = false

Npc.RespawnTime : number

properties_icon RegenerateHealth

Whether the Npc regenerates health over time.

Npc.RegenerateHealth : boolean

The Npc regenerates 1% of the Npc’s Humanoid.MaxHealth per second. It uses the same Health script that Roblox Player characters have by default.


properties_icon PickupToolsOnTouch

Whether the Npc equips tools that happen to touch them.

Npc.PickupToolsOnTouch : boolean

Methods:

Methods are not documented here as they are already documented in the Npc System.


Events:

events_icon Spawned

Fires as soon as the Npc spawns or respawns.

Npc.Spawned ( ) : Signal

Code Samples

Making an Npc spawn at their home

Npc’s by default spawn at their team’s spawn locations, or a neutral spawn location if their Team = nil. The code sample below makes the Npc teleport to their house instead, using the Spawned event.

local NpcClass = require(game.ServerStorage.Npc) -- require the Npc Class

local house = workspace.ClassicHome -- Npc's home model

local npc = NpcClass.new() -- create the Npc
npc.Name = "Jane Doe"
npc.RespawnOnDeath = true
npc.RespawnTime = 0

npc.Spawned:Connect(function() -- Spawned event usage
    npc.Character:MoveTo(house)
end)

events_icon CharacterRemoving

Fires when the Npc’s Character is being destroyed.

Npc.CharacterRemoving ( ) : Signal

Code Samples

Npc.CharacterRemoving

The following code makes sparkles appear in the location of the Npc’s place of destruction.

local NpcClass = require(game.ServerStorage.Npc) -- require the Npc Class

local npc = NpcClass.new() -- create the Npc
npc.Name = "Jane Doe"

npc.CharacterRemoving:Connect(function() -- CharacterRemoving event usage
    local part = Instance.new("Part")
    part.Transparency = 1
    part.Parent = workspace
    
    local sparkles = Instance.new("Sparkles", part)
    
    task.delay(5, function()
        part:Destroy()
    end)
end)

events_icon NavigateFinished

Fires when the Npc finishes walking to a goal declared by NavigateTo().

Npc.NavigateFinished ( reached: boolean ) : Signal

Parameters

reached : boolean
Whether the Npc successfully reached their goal.

Code Samples

Npc.NavigateFinished

The following code prints whether an Npc reached their goal or not.

local NpcClass = require(game.ServerStorage.Npc) -- require the Npc Class

local npc = NpcClass.new() -- create the Npc
npc.Name = "Jane Doe"

npc:NavigateTo(Vector3.new(100, 0, 100)) -- navigate to position: 100, 0, 100

npc.NavigateFinished:Connect(function(reached) -- NavigateFinished event usage
    if reached then
        print(npc.Name .. " reached their goal!")
    else
        print(npc.Name .. " failed to reach their goal...")
    end
end)