I'm making my own ban system for my server and I'm having some problems while attempting to disconnect the player on the 'CheckPassword' hook.
I'm storing bans inside a MySQL Database, when the hook is first run it queries the database to check if they have a valid ban. But it seems to take approx 0.3 seconds to do so. In turn this delay makes it impossible for me to return false and the reason they were banned, inside of the reason it just returns nil because the query hasn't finished. I've tried to use a timer but I'm unsure of how to return false and the ban reason for the hook inside the timer function.
CheckPassword hook:
[CODE]
hook.Add( "CheckPassword", "cBansCheckBan", function( steamid64, ip, sv_password, cl_password, name )
local msg = ""
cBans.Is_cBanned( steamid64, function( banInfo )
if !banInfo[ "access" ] then
msg = "BANNED"
end
msg = "N/A"
end )
return false, msg // this returns nil
end )
[/CODE]
Is Banned function:
[CODE]
function cBans.Is_cBanned( steamid64, func )
local banData = {}
cBans.Query( "SELECT * FROM `bans` WHERE `steamid64` = '" .. steamid64 .. "' AND `length` = '0' OR `expires` >= '" .. os.time() .. "' LIMIT 1" , function( status, data )
banData['access'], banData['steamid64'], banData['reason'], banData['expires'], banData['admin'] = true, nil, nil, nil, nil
if status then
if data[1] != nil then
banData['access'] = false
banData['steamid64'] = data[1]["steamid64"]
banData['reason'] = data[1]["reason"]
banData['expires'] = data[1]["expires"]
banData['admin'] = data[1]["admin"]
end
end
local succ, err = pcall( func, banData )
if err then error( err ) end
end )
end
[/CODE]
SQL Query
[CODE]
function cBans.Query( sql_query, func )
local db = cBans.database
local query = db:query( sql_query )
local status, result
function query:onSuccess( data )
status = true
result = data
if func != false then
local succ, err = pcall( func, status, result )
if err then error( err ) end
end
end
function query:onError( err )
cBans.Print( err )
status = false
result = nil
if func != false then
local succ, err = pcall( func, status, result )
if err then error( err ) end
end
if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
db:connect()
query:start()
end
end
query:start()
end
[/CODE]
Thanks.
You can't return. Use this.
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/game/KickID]game.KickID[/url]
[QUOTE=sannys;51702419]You can't return. Use this.
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/game/KickID]game.KickID[/url][/QUOTE]
Thanks! That will have to do.
Sorry, you need to Log In to post a reply to this thread.