How would I add a timeout to this script

So I have a script which calls a different module, but sometimes that module doesnt return.

	local AssignedTeams = GameModule.AssignTeams(Teams)
	
	

	if AssignedTeams then

So I want to add a timeout that will automatically proceed if the script doesnt get a response from the module for 15 seconds.

So how would I go about doing that

I believe you can just pcall it:

local GameModule = require("GameModule")

local success, errormsg = pcall(GameModule.AssignTeams, Teams)

if not success then
  print("An error occurred:", errormsg)
else
  print("Function executed successfully")
end
1 Like

What if it doesnt give an error message?

Would this work?


	local x = os.time()
	
	

	if AssignedTeams or os.time() - x >= 15 then

		

It doesn’t need to, if it does not run, it does not run. If it ends up in that part of the if statement, you can carry on with your script.

– You can also do that, but if it doesn’t run right away, I doubt it would in 15 seconds lol.

1 Like

But it runs however gets stuck halfway through .

Not sure but i guess you have to do it inside the module if it yields

1 Like

I agree with @richiitalia, you should probably put checks in the actual module to see if it yields or not. However, it seems as if the “AssignTeams” function is important to the actual game. Instead, you should try your best to fix the function so that it will return something no matter the circumstance.

1 Like

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