Better way of spawning multiple objects at once

Overview
Hello, Everyone. I am working on an Obby and wanted to improve how projectile spawns.

  • What does the code do, and what are you unsatisfied with?
    So, the bullet is cloned from the ServerStorage and parented and positioned in the cannon barrel. Then, it is Tweened to the target position on the opposite side. What I am unsatisfied with is I want a way for it to spawn to each cannon one at a time.
  • What potential improvements have you considered?
    I tried running RunService, but I don’t think it’s good for what I’m trying to do. I tried it at first, but I stumbled.
  • How (specifically) do you want to improve the code?
local ServerStorage = game:GetService("ServerStorage")

local cannonFolder = workspace.MarioBullets.Cannons
local bullet = ServerStorage:WaitForChild("Bullet")
local Barrel = cannonFolder.CannonModel.Barrel

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)

local function SpawnBullet(Object: Part, target: Part)
	local cloneBullet = bullet:Clone()
	cloneBullet.Parent = workspace
	game:GetService("Debris"):AddItem(cloneBullet,1)
	cloneBullet.Position =  Object.Position
	local tween = ts:Create(cloneBullet, ti, {Position = target.Position}):Play()
end



while wait(2) do
	for _, v in pairs(cannonFolder:GetChildren()) do
		-- Get the barrel in each cannon model and spawnBullet
		for _, part in pairs(v:GetChildren()) do
			if part.Name == "Barrel" then

				SpawnBullet(part, v.TargetPosition)

			else
				continue
			end
		end
	end
end

Video :


Folder the models in:
image

1 Like

Somewhat vague, but my best estimation is that you want a bullet to be completed with tweening before spawning another.

local function SpawnBullet(Object: Part, target: Part)
	local cloneBullet = bullet:Clone()
	cloneBullet.Parent = workspace
        local finished  -- John, are you done?

    game:GetService("Debris"):AddItem(cloneBullet,1)
	cloneBullet.Position =  Object.Position
	local tween = ts:Create(cloneBullet, ti, {Position = target.Position})
    tween:Play
    tween.Completed:Connect(function()
        finished = true -- Yes, yes I am.
    end)
    repeat task.wait() until finished -- Remove task.wait() for performance boost🤑🤑
end

repe
1 Like

You are the bomb dot com. Thank you. Yes, that solved the problem.

1 Like

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