How to avoid using coroutines for projectile system

the code mostly isn’t mine I just used a tutorial to get a hang of the basics ( The Most ACCURATE Projectile in ROBLOX!! - YouTube

module:

local Projectiles = {}

local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")

Projectiles.new = function(Lifetime, Gravity, Params)
	local Projectile = {}
	
	Projectile.Lifetime = Lifetime
	Projectile.Gravity = Gravity
	Projectile.Params = Params
	
	function Projectile:Cast(Origin, Destination, Velocity, Deviation, Vizualizer)
		
		Vizualizer = Vizualizer:Clone()
		Vizualizer.Parent = workspace
		Vizualizer.Position = Origin
		
		local offset = math.random(-Deviation,Deviation)
		local offset2 = math.random(-Deviation,Deviation)
		local offset3 = math.random(-Deviation,0)

	
		local vForce = (Destination - Origin).Unit * Velocity
		local a = Vector3.new(offset, self.Gravity + offset3, offset2)
		
		local t = 0
		local currentPos = Origin
		local rayResult = nil
		local hit = false
		
		local Connection = RunService.Heartbeat:Connect(function(dt)
			t += dt
			
			local newPos = Vector3.new(
				Origin.X + vForce.X * t + 0.5*a.X*t*t,
				Origin.Y + vForce.Y * t + 0.5*a.Y*t*t,
				Origin.Z + vForce.Z * t + 0.5*a.Z*t*t
			)
			
			--[[
			local tracker = Instance.new("ConeHandleAdornment")
			tracker.Parent = workspace
			tracker.Adornee = workspace.Terrain
			tracker.CFrame = CFrame.lookAt(currentPos,newPos)
			tracker.Height = (currentPos - newPos).Magnitude
			Debris:AddItem(tracker,3)
			]]
			
			rayResult = workspace:Raycast(currentPos, newPos - currentPos, Params)
			currentPos = newPos
			Vizualizer.Position = currentPos
			
			if rayResult or t > self.Lifetime then
				hit = true
				Vizualizer:Destroy()	
			end
		end)
		
		while not hit do
			task.wait()
		end

		Connection:Disconnect()
	end
	
	return Projectile
end

return Projectiles

test client script:

local root = script.Parent:WaitForChild("HumanoidRootPart")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()


local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}

local projectiles = require(game.ReplicatedStorage.Projectiles)
local projectile = projectiles.new(8,-196.2, params)

mouse.Button1Down:Connect(function()
	for i = 1,9 do
		projectile:Cast(root.Position, mouse.Hit.Position, 1573,100,    game.ReplicatedStorage.TracerVizualizer)
	end
end)

for example im firing a shotgun and want to cast 9 projectiles, the next cast has to wait until the first cast finishes because of the task.wait() in the module. what ways are there to solve this without using coroutines?

Try using task.spawn(), or task.delay(), its like a coroute sort of in terms of functionality, they code won’t halt if task.spawn(function) or task.delay(time,function) fails

Why not use coroutines?

The only real way you can achieve this is by spawning new threads for each projectile. You can do this either using coroutines, or the task library.

eh just wanted to know if there would be any better ways to go about this

Why not call a function on the line you do hit = true? Is the only purpose of the wait to disconnect the connection? You can do that safely from inside the callback.

is this what you mean? I get an error saying “Unknown global ‘Connection’; consider signing to it first”

			if rayResult or CurrentDistance > MaxDistance then
				Vizualizer:Destroy()
				Connection:Disconnect()
			end

Oh, right. You need to define Connection first before assigning the RBXScriptConnection to it.

local Connection
Connection = RunService.Heartbeat:Connect(function(dt)

ahh thank you so much lol this is perfect now