FXServer (starting from the alpha update later today: if you’re on the FiveM Discord e-mail email@example.com your nickname and you’ll be added to the group that gets prereleases) will include some enhancements that’ll allow this to work a bit better:
AddEventHandler('playerConnecting', function(name, setCallback, deferrals)
-- mark this connection as deferred
-- save source
local s = source
for i = 1, 20 do
-- of course deferrals can be saved in a local too, just make sure it gets cleared when done!
-- update just sends a status message to the client, that'll show instead of 'Connecting'
deferrals.update('hey, wait ' .. tostring(20 - i) .. ' seconds')
-- accept the client, or reject the connection by passing a reason to `done`
local initHostName = GetConvar('sv_hostname')
while true do
-- can for example be used to show a semi-live pending queue count
-- keep in mind that this will only update on a heartbeat
SetConvar('sv_hostname', '[' .. tostring(#GetPlayers()) .. '] ' .. initHostName)