Justice, the awesome NPC system

Yea I’m also wondering how you would stop the state machine of an npc, so they do nothing anymore

Thanks so much for the continued work on this resource. I’ve used an earlier version (much modified to suit) on our Lost Friends game and found it great to use. Easily customisable and user friendly.

I am in the middle of rewriting and expanding our Monkey Poop game and needed to rewrite the old NPC crowd handler, so hopefully will be using this system.

A quick squizz through the code shows it’s fairly similar to earlier versions so we can easily customise once again.

Curious, can we stop a transition once it’s started? I’m wanting to be able to have panic NPCs and attacking NPCs with this.

btw I still don’t know how to disable the brain of an npc after it dies. If anyone has done it - let me know please

right now I just have checks if the humanoid is dead inside every state and its very ugly and buggy :\

Do u have a .rbxl file that duplicates the issue?

I don’t think its an issue, you may have misunderstood, Im just looking for a way to disable a “brain” (the ai script instance, or whatever it is) wnen my npc humanoid dies

and right now my solution is very very messy

I found the answer and I feel like an idiot now, there’s brain:Stop()
that is literally it…
Took me 4 month to find that jeez I’m blind

2 Likes

Ah lol, thanks for the reply ^^

So I’ve been trying to figure out how to set this up, and there is a clear lack of documentation which is causing me a big headache in trying to figure out how to use this system. My main issue is trying to figure out how to even set up the NPC areas. No matter what I try the NPC’s don’t seem to move at all. I tried tagging every object in the workspace, didn’t work. I tried creating regions using large parts, and tagging those, didn’t work either. The only thing that works for me is just tagging a baseplate on a fresh file.

2 Likes

All my NPC destinations are tagged “Justice:Pathfindable”. This can be found in the RandomPather module (if using Build 6). Any avoid area parts are tagged “Justice:Avoids”. I’ve get an example place with destinations and avoids if you want me to post it up here.

We’ve successfully implemented Build 6 into our new Monkey Poop game and it works well, except for the occasional error when it can’t compute a path, I suspect due to the map size and the destination is too far away.

How did you manage to achieve using brain:Stop(). When I look at the definitions in my brain StateMachine and Hooks, it has no ability to issue a Stop, and I just get an error generated:
image
I need to stop the brain to cleanly destroy the NPCs.

Are you sure you are calling it inside of the startStateMahine function because that’s where brain is defined

For those interested, I added the following to achieve the brain:Stop():

-- In main Justice script 
-- Under function startStateMachine(model: Model) > transitions = {, add the following
			{
				name = "Dead",
				from = { "Wandering", "Routing", "Idling" },
				to = "Died"
			}

startStateMachine(model: Model), add a :Dead hook
	brain:Hook("Dead", function()
-- Remove brain and Model
		brain:Stop()
		brain = nil
		game:GetService("Debris"):AddItem(npcModel, 5)
		npcModel = nil
		return
	end)

You can then transition to this state using brain:Transition("Dead") in the relevant part of your script.

2 Likes

Would I need to do something similar if I were to stop my NPC?

It depends on what you mean by Stop. If you are looking to remove the NPC completely then the Dead transition would work.
tbh, I have moved away from v6 of Justice due to issues with the transition to Dead state as I don’t think my example above worked reliably enough and ended up with NPC corpses left around the place and no replacements would spawn in. I currently use a mixed blend of v4 & some parts of v6.

I meant as in halting the NPC so their pathfinding would stop.

I have tried the usual methods such as waypointing to their current spot and setting walkspeed to 0.

You need to transition to an Idling state. You can do this under the local function startStateMachine(model: Model) by doing:

brain:Transition("Idling")

I don’t purport to fully understand how v6 has been implemented but could get it to do what I wanted within limitation. That’s why I went back to a partial mix of v5 and v6.

What if I wanted it to be triggered by a remote? Sorry if I was vague earlier.

I would add a RemoteEvent to the end of the startStateMachine function to simplify referencing the brain. Something like this might work:

-- Insert after brain:Hooks have been defined
local brainConnect = remoteName.OnServerEvent:Connect(function()
	brain:Idle()
end)
```:
1 Like

it is now skipping the task.wait that is given to them as well and running the main script triggered by the remote server event. And brain:Idle() doesn’t seem to actually stop the pathfinding either.

I just want the specific NPC to stop (triggered by a remote event) before resuming their wandering after the wait is over (when the interaction is over)