Why is spawn doesn't work?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make the function run the countdown while running the script below.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is the spawn function doesn’t work

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I am looking for solutions in DevForum.

function round.Start_Match(length)
	for i = length,0,-1 do
		round.Change_Info(to_MS(i))
		task.wait(1)
	end
	
	spawn(function()
		print("Hello world")
	end)
	
end

image

Thank you@!

This may or may not be an obvious question but is round.Start_Match being called?

Also, because you have the spawn under your for loop, it will not thread until the for loop is completed.

3 Likes

Try to use coroutines or task.spawn instead os just spawn

To get this effect, you’ll need to switch what’s in the spawn function. I recommend using task.spawn because it’s better


function round.Start_Match(length)

task.spawn(function()
	for i = length,0,-1 do
		round.Change_Info(to_MS(i))
		task.wait(1)
	end
end)

		print("Hello world")
	
end

The reason to this is because the loop will yield the code until it’s done, so you want to create a different thread to do that code while you can run other things at the same time.