Spawn() activates too early!

Hello!

I was trying to use spawn() to run 2 loops until i came into a problem.
the function that uses spawn() to be run runs but when it does the timer is already 0 despite the value being 60.
this suggests that the spawn() function runs very very early.
i tried putting the function in the spawn() but it didnt work.

here’s the code:

local function LockdownCountdown()
		for i = BlastdoorTimer, 0, -1 do
			ReactorScreen2.Screen.FacilityLockdownTimer.Timer.Text = "[ "..i.." ]"
		end
		workspace.SoundClose:Play()
	end
	
	spawn(LockdownCountdown)
	
	for i = ExplosionTimer, 0, -1 do
		ReactorScreen1.Screen.EstimatedTime.Timer.Text = "[ "..i.." ]"
		wait(1)
	end
	Sounds["vine boom spam "]:Play()

helpplz

1 Like

Its HIGHLY recommended that you don’t use spawn.

Use task.spawn instead and see if that changes anything.

4 Likes

im gonna try it now. just that the meltdown takes some time to reach phase 2 so please be patient.

2 Likes

uhh sorry this is taking longer than i expected but a power outage happened for a couple of seconds and i disconnected so yeah…

1 Like

spawn() and task.spawn() are used to fire a function or thread immediatley through the Engine Sheduler, they basically have the same functionallity as coroutines which Roblox recommends you look into before using task

The spawn() function is basically Deprecated, so you should be using task.spawn() for it to be faster and to impact less on performance, if you dont want it to fire immediatley, try task.defer().

2 Likes

Actually, spawn is equivalent to task.defer, not task.spawn. spawn will run the thread in the next frame, while task.spawn will not.

2 Likes

I dont recall saying that.

1 Like

Yes. spawn will not run the thread immediately. Therefore, it is equivalent to task.defer.

2 Likes

it doesn’t work. i tried task.spawn but the countdown still says 0.

it doesn’t work. i tried task.spawn but the countdown still goes to 0

That is not how you use spawn().

Also spawn() and wait() are deprecated use task.spawn() for code that needs to run as soon as possible and task.defer() for everything else. For yielding use task.wait()

local function LockdownCountdown()
	
	task.spawn(function()
	
	   for i = ExplosionTimer, 0, -1 do
		   ReactorScreen1.Screen.EstimatedTime.Timer.Text = "[ "..i.." ]"
		   task.wait(1)
	   end
	   Sounds["vine boom spam "]:Play()

    end)

		for i = BlastdoorTimer, 0, -1 do
			ReactorScreen2.Screen.FacilityLockdownTimer.Timer.Text = "[ "..i.." ]"
                        task.wait(1)
		end
		workspace.SoundClose:Play()
	end

2 Likes

Damn, thank you very much!
I will use task.spawn() this way from now on I guess.

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