Does game:BindToClose() prevent players from joining after it has been executed?

Well, I pretty much already explained it on the title. Just wanted to know if a game that is closing will prevent any players from joining while the callbacks are running.

game:BindToClose(function()) just fires when a server shutdowns. When a server shutdowns its completely locked and no one can join.

Simply put: No.

The only thing game:BindToClose() does is give a (30 second maximum) period where any connected code can run before shutting down the server. Once the thread completes, the server will close.

However, if the thread does not complete within 30 seconds, the server will close regardless of the current task’s state.

Is there a way to calculate how long it took for the thread to finish? either using os.clock() or tick()
Asking this for DataStore saving purposes.

As far as I’m aware, no… :frowning_with_open_mouth:

tick() simply counts up by one every second since January 1st, 1970. It, nor os.clock() is linked with execution times.

Plus, keep in mind that every time a datastore is accessed or set, the game server must contact Roblox’s datastore servers. This could take a range of time, depending on the connection speed, server load, bandwidth usage, etc.

Edit:
I may have misunderstood your question, apologies. The answer above is regarding calculation of how long it will take to run a thread.

Luckily, there is a way to calculate how long a thread took to run. And you can use tick()! :smile:

Here’s an example of how you can use tick() to calculate the amount of time a thread took to finish:

local currentTime = tick()

wait(math.random(1,10))

print("The thread took approximately "..math.floor(tick()-currentTime).." Seconds.")

The printout:
Screenshot_20230130_081142

It is possible. Just did some research.

local __STARTTIME = os.time()
local __ENDTIME = os.time()
local __TIMETITOOK 
function __FUNC()
    __STARTTIME = os.time()
       -- thread
   __ENDTIME = os.time()
   __TIMETITOOK = os.difftime(__STARTTIME,__ENDTIME )
   print(__TIMEITOOK)
end
1 Like

@Katrist @bonkybincer I am sorry I must have explained it incorrectly, I tried to make it easier to explain.
When I mean :BindToClose I don’t actually mean the function itself but the moment the game ends, does it lock the server and prevent NEW players from JOINING and/or kick players that are already in, in a rare case?

1 Like

It prevents new players from joining and kicks out players who were already in. You don’t have to worry about someone joining while the server is shutting down, because they will be kicked with a message saying something like “This server has been shutdown”

2 Likes

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