How to make NPC Line System

  1. What do you want to achieve? I want to create an NPC Line System

  2. What solutions have you tried so far?
    Searched all over the devforum but can’t find a simple answer

  3. Any support will be great, thanks!

4 Likes

Are you referring to dialogue, pathfinding, or something else? Sorry if it comes across as nitpicky, but the term ‘line’ doesn’t really click to anything for me.

1 Like

So like a line system, where an npc comes to pay at a till and then another npc comes and goes behind the one who’s paying and then another goes behind the other and then it goes on and on.

If you want to line a bunch of NPCs up, using Humanoid:MoveTo with relative destination values on several humanoids should do the trick.

Here’s an annotated script that pretty much covers everything:

local debris = game:GetService'Debris';

local npc = game.ServerStorage:WaitForChild'Rig'; --can be chosen at random instead

local origin = Vector3.new(20, 0, 20); --place in which our npcs will spawn at
local exit = -origin; --place in which our npcs leave to

local line = { --storage of all our variables that have to do with the line, i find the script to be more readable with this
	
	start = Vector3.new(0, 0, 0); --value that represents the location of the start of the line
	
	spacing = 2; --value that represents the distance between each participant of the queue
	
	direction = Vector3.new(1, 0, 0); --value that represents the direction of the line. in this case the line extrudes out in the x-axis
	
	queue = {}; --table of humanoids in our line (not the model, the actual humanoid object)
};

local function update()
	for i, attendant in line.queue do
		attendant:MoveTo(line.start + line.direction * line.spacing * (i - 1)); --calculate the physical destination of our npc
	end
end

task.defer(function() --handle removing npcs from the line
	while task.wait(10 / math.max(#line.queue, 1)) do --deletion accelerates based on the queue size (so as to not make it infinitely grow)
		local departer = table.remove(line.queue, 1); --remove the person in the front of the line, table.remove returns that person if they exist
		
		if not departer then continue; end --escape this iteration if there was nobody in the front of the line
		
		departer:MoveTo(exit); --make the npc walk to our exit
		
		update(); --update the humanoid's positions
		
		debris:AddItem(departer.Parent, (exit - line.start).Magnitude / departer.WalkSpeed);
	end
end)

while task.wait(2) do --handle adding npcs to the line
	local addition = npc:Clone(); --make a new npc
	local hum = addition:WaitForChild'Humanoid';
	
	addition.Parent = workspace;
	
	addition:PivotTo(CFrame.new(origin + Vector3.new(0, hum.HipHeight))); --spawn our npc at the origin and translate him/her upwards according to their height
	
	line.queue[#line.queue + 1] = hum; --add our humanoid to the queue
	
	update(); --update the humanoid's positions
end

If something doesn’t make sense, I can probably explain it. Here’s what my product looked like:

3 Likes

This is exactly what i want, Thanks, Let me try to implement this system into my game!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.