Npc
An OOP class for Non-Player Characters.
Get the Npc class here: create.roblox.com/store/asset/80973226380705/Npc-System
Summary
Properties:
Methods:
NavigateTo( location: Vector3, pathfind: boolean, agentParameters: Dictionary ) : ( )
CancelNavigation ( ) : ( )
SetFace( imageId: string, duration: number ) : ( )
Dance( danceNum: number ) : ( )
UnequipTools ( ) : ( )
ActivateTool ( ) : ( )
Ragdoll( duration: number, pushMagnitude: number) : ( )
EnableShiftLock( orientation: Vector3, lookAt: Vector3 ) : ( )
DisableShiftLock ( ) : ( )
SetDialog( dialog: Dialog ) : ( )
Respawn( location: Vector3 ) : ( )
TakeDamage( amount: number ) : ( )
LoadAnimation( animation: animation ) : ( AnimationTrack )
AddAccessory( accessory: Instance ) : ( )
Events:
CharacterRemoving( ) : Signal
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:
Character
A reference to the Npc’s character model.
Npc.Character : Model |
|---|
Name
The name of the Npc. This is identical to Npc.Character.Name
Npc.Name : string |
|---|
Health
The amount of health the Npc’s humanoid currently has. This is identical to Humanoid.Health
Npc.Health : number |
|---|
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 |
|---|
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 |
|---|
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 |
|---|
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 |
|---|
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.
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:
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)
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)
NavigateFinished
Fires when the Npc finishes walking to a goal declared by NavigateTo().
Parameters
reached : boolean
Whether the Npc successfully reached their goal.
| 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)