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:
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)
moveTopart.parent -> HEREfor 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
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?
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!