Hello, I'm trying to mod ULib a bit, by adding each ban into a mysql database. The problem is i'm getting an error and I don't know why. Here's my ban function:
[code]
function ULib.addBan( steamid, time, reason, name, admin )
local strTime = time ~= 0 and string.format( "for %s minute(s)", time ) or "permanently"
local showReason = string.format( "Banned %s: %s", strTime, reason )
local players = player.GetAll()
for i=1, #players do
if players[ i ]:SteamID() == steamid then
ULib.kick( players[ i ], showReason, admin )
end
end
-- This redundant kick code is to ensure they're kicked -- even if they're joining
game.ConsoleCommand( string.format( "kickid %s %s\n", steamid, showReason or "" ) )
game.ConsoleCommand( string.format( "banid %f %s kick\n", time, steamid ) )
game.ConsoleCommand( "writeid\n" )
local con = mysqloo.connect("host", "user", "*****", "db", 3306)
>>>>> error here >>>>> local AddBan = con:query("INSERT INTO bans (BanSID, BanTime, Reason, Username, BannedBy) VALUES ('" ..steamid.. "', '" ..time.. "', '" ..reason.. "', '" ..name.. "', '" ..admin:Nick().. "')")
local admin_name
if admin then
admin_name = "(Console)"
if admin:IsValid() then
admin_name = string.format( "%s(%s)", admin:Name(), admin:SteamID() )
end
end
local t = {}
if ULib.bans[ steamid ] then
t = ULib.bans[ steamid ]
t.modified_admin = admin_name
t.modified_time = os.time()
else
t.admin = admin_name
end
t.time = t.time or os.time()
if time > 0 then
t.unban = ( ( time * 60 ) + os.time() )
else
t.unban = 0
end
if reason then
t.reason = reason
end
if name then
t.name = name
end
ULib.bans[ steamid ] = t
ULib.fileWrite( ULib.BANS_FILE, ULib.makeKeyValues( ULib.bans ) )
AddBan:start()
end
[/code]
The error:
[code]
[ERROR] addons/ulib/lua/ulib/server/player.lua:233: attempt to index local 'AddBan' (a nil value)
1. addBan - addons/ulib/lua/ulib/server/player.lua:233
2. unknown - addons/ulx/lua/ulx/xgui/server/sv_bans.lua:92
3. unknown - addons/ulx/lua/ulx/modules/xgui_server.lua:53
4. unknown - lua/includes/modules/concommand.lua:69
[/code]
Thank you!
[quote]
[ERROR] addons/ulib/lua/ulib/server/player.lua:233: attempt to index local 'AddBan' (a nil value)
[/quote]
[quote]
function ULib.addBan( steamid, time, reason, name, admin )
[/quote]
The capitalization is different, this may be the problem, unless there's two different functions.
Not sure, never really messed around with ULX.
[B]I'm fucking blind, disregard that ^[/B]
I believe the problem is that this line:[code] local AddBan = con:query("INSERT INTO bans (BanSID, BanTime, Reason, Username, BannedBy) VALUES ('" ..steamid.. "', '" ..time.. "', '" ..reason.. "', '" ..name.. "', '" ..admin:Nick().. "')")[/code]
Return nil, so this line
[code]AddBan:start()[/code] throws an error because AddBan doesn't exist.
[QUOTE=Jeezy;43836919]The capitalization is different, this may be the problem, unless there's two different functions.
Not sure, never really messed around with ULX.[/QUOTE]
AddBan is seperate from addBan:
All the AddBan code:
[code]
local con = mysqloo.connect("host", "user", "*****", "db", 3306)
local AddBan = con:query("INSERT INTO bans (BanSID, BanTime, Reason, Username, BannedBy) VALUES ('" ..steamid.. "', '" ..time.. "', '" ..reason.. "', '" ..name.. "', '" ..admin:Nick().. "')")
AddBan:start()
[/code]
Also, if it helps, [URL="https://facepunch.com/showthread.php?t=1220537"]this is the mysqloo version i'm using[/URL].
I would connect earlier, and save the connection for later. Otherwise, you'll need to hook into con.onConnected everytime.
[QUOTE=Robotboy655;43836938]I believe the problem is that this line:[code] local AddBan = con:query("INSERT INTO bans (BanSID, BanTime, Reason, Username, BannedBy) VALUES ('" ..steamid.. "', '" ..time.. "', '" ..reason.. "', '" ..name.. "', '" ..admin:Nick().. "')")[/code]
Return nil, so this line
[code]AddBan:start()[/code] throws an error because AddBan doesn't exist.[/QUOTE]
I even tried doing
[code]
con:query("INSERT INTO bans (BanSID, BanTime, Reason, Username, BannedBy) VALUES ('" ..steamid.. "', '" ..time.. "', '" ..reason.. "', '" ..name.. "', '" ..admin:Nick().. "')"):start()
[/code]
But then I get
[code]
[ERROR] addons/ulib/lua/ulib/server/player.lua:227: attempt to index a nil value
1. addBan - addons/ulib/lua/ulib/server/player.lua:227
2. unknown - addons/ulx/lua/ulx/xgui/server/sv_bans.lua:92
3. unknown - addons/ulx/lua/ulx/modules/xgui_server.lua:53
4. unknown - lua/includes/modules/concommand.lua:69
[/code]
[editline]8th February 2014[/editline]
[QUOTE=Bo98;43836990]I would connect earlier, and save the connection for later. Otherwise, you'll need to hook into con.onConnected everytime.[/QUOTE]
I tried that earlier, the only reason the con variable is inside the ban function is I wanted to see if the connection is the problem, but it's in fact not.
If you are putting it in the ban function then you'll need to wait for it to connect.
[lua]
local con = mysqloo.connect("host", "user", "*****", "db", 3306)
con.onConnected = function()
-- query
end
con:connect()
[/lua]
[QUOTE=Bo98;43837051]If you are putting it in the ban function then you'll need to wait for it to connect.
[lua]
local con = mysqloo.connect("host", "user", "*****", "db", 3306)
con.onConnected = function()
-- query
end
con:connect()
[/lua][/QUOTE]
[lua]
con.onConnected = function(con)
print("Ban added!")
self:query("INSERT INTO Bans (BanSID, BanTime, UnbanTime, Reason, Username, BannedBy) VALUES ('" ..steamid.. "', '" ..time.. "', '" ..t.unban.. "', '" ..t.reason.. "', '" ..t.name.. "', '" ..tostring(admin).. "')"):start()
end
con:connect()
[/lua]
This is inside ULib.addBan().
Is it right? Sorry i'm in a hurry to get this working
Sorry, you need to Log In to post a reply to this thread.