Spawning Multiple Dummies and Making Them Move

Hello developers! I am making a system for a game where when I activate a tool, a dummy spawns and walks to a specific point (designated by the MoveToPart). The system currently works, but I was wondering how I would make it so a line of dummies would spawn, and they would all move at the same time to different MoveToParts infront of them. I don’t really know where to start for this.

Here is a drawing of how I would like the dummies to line up:

Here is the explorer window with most of the things in side of it:
Explorer Window

Here is the code inside of the tool:

local function generateEnemy()
	local moveToPart = game.ReplicatedStorage.MoveToPart:Clone()
	moveToPart.Parent = workspace
	local dummy = game.ReplicatedStorage.Enemy:Clone()
	dummy.Parent = workspace

	local humanoid = dummy:FindFirstChildOfClass("Humanoid")

	if humanoid then

		local walkAnim = Instance.new("Animation")
		walkAnim.AnimationId = "rbxassetid://180426354"

		local animTrack = humanoid:LoadAnimation(walkAnim)

		animTrack:Play()

		humanoid:MoveTo(moveToPart.Position)

		humanoid.MoveToFinished:Connect(function()
			animTrack:Stop()
			dummy:Remove()
			moveToPart:Remove()
		end)
	end
end

script.Parent.Activated:Connect(function()
	
	generateEnemy()
	
end)

Just tell me if you need any more information!

Make a loop spawning multiple dummies wrapped around your current code.

  • I’d suggest using repeat until with a counter or for i = 1,x do
  • Now, your code will just repeat x amount of times.

Essentially, just placing your code inside the function in an iterator.

How would I make it so they line up next to eachother? Also do you think you could show me where to put the code?

right after MovetoPart

moveTopart.parent
-> HERE for i = 1, #of dummies do

To make them line up, simply track the offset.

  • If we have 5 dummies, and we want them 3 studs apart, we could just say dummy 1 will be 6 studs from the center.
  • Then we add 3 every time to the same variable, so dummy 2 will be 3 studs, 3 will be 0 studs, 4 will be +3 studs the other way, and 5 will be +6 studs the other way
  • You could achieve this with a variable = 6, then set the dummy’s position to the position + Vector3.new(0,0,variable)
  • Then take away from that variable each step, i said 3 so variable -= 3

then add an extra end at the bottom

Can you explain what you mean by “different MoveTo parts”? Also, when you generate the line of dummies, do you expect it to start from the center point or from a end point? :cool:

Assuming you wanted the line to be spawned from a specified starting point, this code should work.

--!strict

function GenerateEnemy(Position: Vector3): ()
	
	local moveToPart = game.ReplicatedStorage.MoveToPart:Clone() :: Part
	moveToPart.Parent = workspace
	
	local dummy = game.ReplicatedStorage.Enemy:Clone() :: Model
	dummy.Parent = workspace
	
	dummy:PivotTo(CFrame.new(Position))
	moveToPart:PivotTo(CFrame.new(Position + Vector3.new(0, 0, 10))) -- 10 Studs forward on the Z axis.

	local humanoid = dummy:FindFirstChildOfClass("Humanoid")

	if humanoid then

		local walkAnim = Instance.new("Animation") :: Animation
		walkAnim.AnimationId = "rbxassetid://180426354"

		local animTrack = humanoid:LoadAnimation(walkAnim) :: AnimationTrack

		animTrack:Play()

		humanoid:MoveTo(moveToPart.Position)

		humanoid.MoveToFinished:Connect(function()
			animTrack:Stop()
			dummy:Remove()
			moveToPart:Remove()
		end)
	end
end

function GenerateEnemyLine(StartingPoint: Vector3, Count: number, Spacing: number): ()
	for X: number = 1, Count do
		GenerateEnemy(StartingPoint + Vector3.new(X * Spacing, 0, 0))
	end
end

script.Parent.Activated:Connect(function()
	GenerateEnemyLine(Vector3.new(0, 3, 0), 5, 3)
end)

If you want an explanation to how the code works, feel free to ask! :coefficients: