Running code in parallell

Anybody know how to make my while loop run at the same time as the funtion or is this impossible :

local Debris = game:GetService("Debris")

local part = game.ReplicatedStorage.Part
local origin = workspace.Origin
local radius = 30

local function SpawnPart()
	local speed = math.random(4, 16)/10
	local height = math.random(4, 16)/10
	local horizontalSpeed = math.random(4, 16)/10
	local lifeTime = 10

	local targetPart = part:Clone()
	targetPart.Parent = workspace.Aurora

	targetPart.Position = origin.Position + Vector3.new(math.random(-radius/2, radius/2), math.random(-radius/2, radius/2), math.random(-radius/2, radius/2))
	Debris:AddItem(targetPart, lifeTime)

	local i = 1
	while targetPart do
		targetPart.Position = targetPart.Position + Vector3.new(.1*horizontalSpeed, math.sin(math.rad(i*speed))*height/36, math.sin(math.rad(i*speed))*height/36)
		i += 1
		task.wait(.001)
	end
end

while wait(1) do
	print("spawn")
	spawn(SpawnPart())
end

Spawn is being printed once every 10 seconds instead of every second

You should be using task.spawn instead: task.spawn(SpawnPart)

Instead of doing this:

You would do:

task.spawn(function() -- Also task.spawn is more efficient than spawn (b/c of task library)
	while true do
        print("Spawn")
	    SpawnPart()
end

You have to wrap to the while loop inside of the spawn.

That didnt fix it it still prints and spawns 1 block every 10 seconds

Well i figured it out all I had to do was transfer all the movement code to a different script under the part and clone the part with the scripts inside. Thanks for the help though! made me realise this was the wrong way to go about it :slight_smile:

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