How to make NPC Line System

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:

5 Likes