Why is my functions not running at the same time?

I made a module so that it would be easier for me to fire functions for a music game.

But Im having an issue where, the waits inside of the function make the script only run one function at a time.

How would I fix this?

module:

function module.slider(zone)
	if zone == "zoneL" then
		local clonedAttack = sliderAttack:Clone()
		clonedAttack.Position = zoneL.Position
		clonedAttack.Parent = game.Workspace
		clonedAttack.Orientation = zoneL.Orientation
		clonedAttack.Size = zoneL.Size

		for i = 1, 26 do
			task.wait()
			clonedAttack.Transparency += 0.04
			clonedAttack.Size += Vector3.new(0,0,2)
		end

		task.wait(0.1)

		clonedAttack:Destroy()
	end

	if zone == "zoneR" then
		local clonedAttack = sliderAttack:Clone()
		clonedAttack.Position = zoneR.Position
		clonedAttack.Parent = game.Workspace
		clonedAttack.Orientation = zoneR.Orientation
		clonedAttack.Size = zoneR.Size

		for i = 1, 26 do
			task.wait()
			clonedAttack.Transparency += 0.04
			clonedAttack.Size += Vector3.new(0,0,2)
		end

		task.wait(0.1)

		clonedAttack:Destroy()
	end

	if zone == "zoneU" then
		local clonedAttack = sliderAttack:Clone()
		clonedAttack.Position = zoneU.Position
		clonedAttack.Parent = game.Workspace
		clonedAttack.Orientation = zoneU.Orientation
		clonedAttack.Size = zoneU.Size

		for i = 1, 26 do
			task.wait()
			clonedAttack.Transparency += 0.04
			clonedAttack.Size += Vector3.new(0,0,2)
		end

		task.wait(0.1)

		clonedAttack:Destroy()
	end

	if zone == "zoneD" then
		local clonedAttack = sliderAttack:Clone()
		clonedAttack.Position = zoneD.Position
		clonedAttack.Parent = game.Workspace
		clonedAttack.Orientation = zoneD.Orientation
		clonedAttack.Size = zoneD.Size

		for i = 1, 26 do
			task.wait()
			clonedAttack.Transparency += 0.04
			clonedAttack.Size += Vector3.new(0,0,2)
		end

		task.wait(0.1)

		clonedAttack:Destroy()
	end
end

you can use this funny magic

task.spawn(function()
       --execute module thing here
end)

it allows you to run threads separately like coroutines

1 Like

So do I put it inside of the function itself and then run the info given?

you put the function inside it then you run it

1 Like

So do I do this and then call the function?

task.spawn(function()
	function module.slider(zone)
		if zone == "zoneL" then
			local clonedAttack = sliderAttack:Clone()
			clonedAttack.Position = zoneL.Position
			clonedAttack.Parent = game.Workspace
			clonedAttack.Orientation = zoneL.Orientation
			clonedAttack.Size = zoneL.Size

			for i = 1, 26 do
				task.wait()
				clonedAttack.Transparency += 0.04
				clonedAttack.Size += Vector3.new(0,0,2)
			end

			task.wait(0.1)

			clonedAttack:Destroy()
		end

		if zone == "zoneR" then
			local clonedAttack = sliderAttack:Clone()
			clonedAttack.Position = zoneR.Position
			clonedAttack.Parent = game.Workspace
			clonedAttack.Orientation = zoneR.Orientation
			clonedAttack.Size = zoneR.Size

			for i = 1, 26 do
				task.wait()
				clonedAttack.Transparency += 0.04
				clonedAttack.Size += Vector3.new(0,0,2)
			end

			task.wait(0.1)

			clonedAttack:Destroy()
		end

		if zone == "zoneU" then
			local clonedAttack = sliderAttack:Clone()
			clonedAttack.Position = zoneU.Position
			clonedAttack.Parent = game.Workspace
			clonedAttack.Orientation = zoneU.Orientation
			clonedAttack.Size = zoneU.Size

			for i = 1, 26 do
				task.wait()
				clonedAttack.Transparency += 0.04
				clonedAttack.Size += Vector3.new(0,0,2)
			end

			task.wait(0.1)

			clonedAttack:Destroy()
		end

		if zone == "zoneD" then
			local clonedAttack = sliderAttack:Clone()
			clonedAttack.Position = zoneD.Position
			clonedAttack.Parent = game.Workspace
			clonedAttack.Orientation = zoneD.Orientation
			clonedAttack.Size = zoneD.Size

			for i = 1, 26 do
				task.wait()
				clonedAttack.Transparency += 0.04
				clonedAttack.Size += Vector3.new(0,0,2)
			end

			task.wait(0.1)

			clonedAttack:Destroy()
		end
	end
end)

i mean like this for example

task.spawn(function()
    module.sliderthing(zonePart)
end)
1 Like

it’s an example, you just replace it with the usual way of how you trigger module functions

Do I do this in the script thats calling the functions then?

task.spawn(function()
	attackModule.slider("center")
end)

attackModule.slider("zoneL")
attackModule.slider("zoneR")

attackModule.slider("zoneU")
attackModule.slider("zoneD")
attackModule.spinner("center")

actually nevermind, i think this one will work more efficiently

function module.slider(zone)
	task.spawn(function()
       if zone == "zoneL" then
		local clonedAttack = sliderAttack:Clone()
		clonedAttack.Position = zoneL.Position
		clonedAttack.Parent = game.Workspace
		clonedAttack.Orientation = zoneL.Orientation
		clonedAttack.Size = zoneL.Size

		for i = 1, 26 do
			task.wait()
			clonedAttack.Transparency += 0.04
			clonedAttack.Size += Vector3.new(0,0,2)
		end

		task.wait(0.1)

		clonedAttack:Destroy()
	end

	if zone == "zoneR" then
		local clonedAttack = sliderAttack:Clone()
		clonedAttack.Position = zoneR.Position
		clonedAttack.Parent = game.Workspace
		clonedAttack.Orientation = zoneR.Orientation
		clonedAttack.Size = zoneR.Size

		for i = 1, 26 do
			task.wait()
			clonedAttack.Transparency += 0.04
			clonedAttack.Size += Vector3.new(0,0,2)
		end

		task.wait(0.1)

		clonedAttack:Destroy()
	end

	if zone == "zoneU" then
		local clonedAttack = sliderAttack:Clone()
		clonedAttack.Position = zoneU.Position
		clonedAttack.Parent = game.Workspace
		clonedAttack.Orientation = zoneU.Orientation
		clonedAttack.Size = zoneU.Size

		for i = 1, 26 do
			task.wait()
			clonedAttack.Transparency += 0.04
			clonedAttack.Size += Vector3.new(0,0,2)
		end

		task.wait(0.1)

		clonedAttack:Destroy()
	end

	if zone == "zoneD" then
		local clonedAttack = sliderAttack:Clone()
		clonedAttack.Position = zoneD.Position
		clonedAttack.Parent = game.Workspace
		clonedAttack.Orientation = zoneD.Orientation
		clonedAttack.Size = zoneD.Size

		for i = 1, 26 do
			task.wait()
			clonedAttack.Transparency += 0.04
			clonedAttack.Size += Vector3.new(0,0,2)
		end

		task.wait(0.1)

		clonedAttack:Destroy()
	     end
    end)
end
2 Likes