Is it possible to bypass wait() functions and grab other blocks of code while it's happening within the same script? (Or are there any alternatives?)

I’m currently writing a module script with several mechanics that get called by the main server handler script that handles them. But I have an issue where the wait function completely blocks the other scripts. (It’s also in a repeat loop, as it’s for a time based mechanic that has the time changed). I tried moving the other code above it but when it eventually reached that point, it just didn’t change anything.

Just wanting to figure out a way to make it so I can get other blocks of code within a script while a wait() function is happening, any alternatives or help would be much appreciated.

The mechanic module script:

local Mechanics = {
	
	Errors = {
		Ventilation = function()
			print("Ventilation going off.")
		end,

		Audio = function()
			print("Audio going off.")
		end,

		Camera = function()
			print("Camera shut down.")
		end,
	},
	
	timeChip = function(coolNumber)
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local timeInt = ReplicatedStorage.Values:WaitForChild("Time")
		timeInt.Value = coolNumber
		repeat
			repeat wait() until ReplicatedStorage.Values.NightBegun.Value == false
			wait(5)
			print("The time is currently: ".. coolNumber.. "AM")
			coolNumber += 1
			if coolNumber > 12 then
				coolNumber = 1
			end
		until false
	end,
}

return Mechanics

The game handler script:

-- setting up all the module scripts

local Mechanics = require(script.MechanicsLogic)
local Behaviour = require(script.BehaviourLogic)

-- main variables needed
local ReplicatedStorage = game:GetService("ReplicatedStorage")
wait()

-- making sure Time works
local timeInt = ReplicatedStorage.Values:WaitForChild("Time")
Mechanics.timeChip(12)

-- Errors (ALL IN ONE CATAGORY)

spawn(Mechanics.Errors.Ventilation)

return Mechanics

Thank you for reading.

It looks like you need coroutines…

Coroutines let you run codes while certain things happen. And don’t worry because coroutines are not that difficult you can watch some tutorials about them.

1 Like

Coroutines is probably what would be most recommended by many. However for a quick and easy solution you could do the same with task.spawn()

1 Like

Wait so, it’ll pretty much allow me to run the block of code and run the wait() function at the same time?

You would need to use the task and coroutine libraries. Both of them are specially made for stuff like that. Note that you should use the task library instead of regular global functions. (e.g: using task.wait() instead of wait() and using task.delay() instead of delay())

And if you ask me, Why using the task library over the regular wait/spawn/delay functions? Simple, The task library is a way better and updated version of those functions. They’re also way more precise than the old ones.

You can learn more about both of the libraries here:

1 Like

I tried using task.wait and task.spawn and it didn’t seem to change anything, hm. Maybe I’m doing something wrong?

What is exactly not working? Also, The only difference is that the libraries (like i said) are more precise and optimized. If possible, Can you go further and explain what you exactly want to do?

1 Like

My main goal is basically to have it so when this specific function is called from the ModuleScript, it can bypass the Clock timer being used for the game’s time mechanic to still run that code while the time mechanic is still going.

You can use task.spawn() for this:

task.spawn(function()
    -- Function/Loop that yields
end)
1 Like

Do I also turn all of the wait() functions into task.wait()?

That’s really up to you. But if you care alot about performance and want more precise results, You can do that. (I highly recommend you to do so)

1 Like

Didn’t seem to change anything. Still doesn’t run.

Maybe it’s the repeat loop within the game’s time function?

Yep, I think so. Maybe you could try changing it to a while loop and spawn it using the task spawn function?

1 Like

This worked! Thank you so much!

1 Like