My ModuleScript only runs after the last one is done

My ModuleScript is called, ran, then waits until the Modulescript is done to run again. It is supposed to run 25 times in a second (to spawn 25 things, but they all need scripts in them blah blah blah just trust me I need a Modulescript for this) but it runs the first one, then waits till the first one is done to run the second one.

local module = {}

function module.FlyDownAndExplode()
	if game:GetService("ReplicatedStorage").RegularArtilleryShelling.Value == true then
		local artyshell = script.Parent.ArtyShell:Clone()
		local tweenservice = game:GetService("TweenService")

		artyshell.Parent = workspace
		artyshell.WhistleSound:Play()
		artyshell.BillboardGui.Enabled = true
		artyshell.CFrame = CFrame.new(math.random(-917, 950), 801.22, math.random(-1002, 980))
		repeat
			local yPos = artyshell.Position.Y
			local goal = {}
			goal.Position = Vector3.new(artyshell.CFrame.X, yPos - 15, artyshell.CFrame.Z)
			local info = TweenInfo.new(0.1)
			local tween = tweenservice:Create(artyshell, info, goal)
			tween:Play()
			wait(0.05)
		until artyshell.CFrame.Y <= 0

		artyshell.BillboardGui.Enabled = false
		artyshell.ExplosionSound:Play()
		artyshell.WhistleSound:Stop()
		repeat
			artyshell.CFrame = CFrame.new(artyshell.CFrame.X, artyshell.CFrame.Y + 2.5, artyshell.CFrame.Z)
			wait(0.00000000000001)
		until artyshell.CFrame.Y >= 5
		artyshell.Explosion:Emit(150)
		artyshell.Explosion:Emit(150)
		game:GetService("Debris"):AddItem(artyshell, 5.01)
	end
end

return module

Help.

You need to use some sort of threading, the task library should do the job.

task.spawn(module.FlyDownAndExplode) or task.defer(module.FlyDownAndExplode), probably sticking with the first unless you understand why/when you’d want to defer.

1 Like

Excuse me, one question, what?

Well, it did work, so thank you. Don’t get it, but thanks.

Everything by default runs on a single thread, which means scripts run sequentially (if something blocks, another will run). Your options are to use pseudo multi-threading (scheduling through task spawn/defer/delay, coroutines), or true multi-threading (task sync/desync); but the latter is overkill and not fully featured (has pain points and limitations).

1 Like