General > Developers Corner
Error in External MySQL database config - bad key
jacksop:
Hey!
So I have connected my server to an external mysql database using the mysqloo9 module and have populated it with information such as player steamids.
I also want to log the ULX rank/user group on this database to help keep track, however I have run into an issue with the code.
Here is the code:
--- Code: ---function DBULXRank (ply, currentulxrank)
local query2 = DB_RP:query( "SELECT ulx_rank FROM playerinformation WHERE steam_id = '" .. ply:SteamID() .. "';") -- checking current data
query2.onSuccess = function()
if (#query2:getData() != ply:GetUserGroup()) then -- if the data does not equal the in-game usergroup
local query = DB_RP:query("UPDATE playerinformation SET ulx_rank = '".. currentulxrank .."' WHERE steam_id = '" .. ply:SteamID() .. "';") --update the database column called ulx_rank
query.onSuccess = function()
MsgC( Color (0, 0, 255), "[SQL] The new player's rank " .. currentulxrank .. " was logged! \n")
end
query.onError = function(db, err)
MsgC( Color (255, 0, 0), "[SQL] (Add Player) - Error: ", err)
end
query:start()
end
end
query2:start()
end
hook.Add("PlayerInitialSpawn", "UpdateULXrole", function(ply)
DBULXRank(ply:GetUserGroup())
end)
--- End code ---
What I want this function to do is when the player spawns in/connects, i want to check the database if the ULX rank is the correct one that is in-game, if not then update it.
The error is:
attempt to index a string value with bad key ('SteamID' is not part of the string library)
How can I fix this? Let me know if you need more information.
Please keep in mind that I am new to lua so if theres an obvious problem please let me know how to solve it!
Thanks in advance
jacksop:
Based on some testing I did, the cause of this problem is when I try to retrieve and concatenate ply:SteamID.
I am assuming the error: "attempt to index a string value with bad key" means I cannot concatenate this value as a string but rather as a variable.
So i changed my query line to this:
--- Code: --- local query2 = DB_RP:query( "SELECT ulx_rank FROM playerinformation WHERE steam_id = '" .. ply:SteamID() "';") -- no dots after ply:SteamID()
--- End code ---
Doing so gives me this error instead: attempt to call a string value
So I guess my ultimate question here is.. how does lua respond to what value the ply:SteamID() is? is a treated as a variable? string?
Am I on the right track here or just going off on pointless tangents? :D :-\
Timmy:
--- Code: ---attempt to index a string value with bad key
--- End code ---
The error is a little obscure. It means that you are trying to call a nonexistent method on a string.
Take a look at the example below.
--- Code: ---local nickname = "Timmy"
nickname:SteamID() -- Error: attempt to index a string value with bad key ('SteamID' is not part of the string library)
--- End code ---
The :SteamID() only works on Players, not strings. Your ply is actually a string.
Have a look at the hook that calls DBULXRank. Make sure you give it all the necessary arguments.
jacksop:
Hey thanks for the reply!
Ive slightly edited the hook to what I want it and changed the concatenation for the string in the function.
The hook is now this:
--- Code: ---hook.Add("PlayerDisconnected", "UpdateULXrole", function(ply)
DBULXRank(ply, ply:GetUserGroup())
end)
--- End code ---
The bad key error seems to be fixed! However, now i get this error:
--- Code: ---lua/autorun/server/sv_database.lua:100: Tried to use a NULL entity!
stack traceback:
[C]: at 0x647e9220
[C]: in function 'SteamID'
lua/autorun/server/sv_database.lua:100: in function <lua/autorun/server/sv_database.lua:97>
[C]: in function 'fn'
addons/ulib-v2_63/lua/ulib/shared/hook.lua:109: in function <addons/ulib-v2_63/lua/ulib/shared/hook.lua:92>
--- End code ---
FYI - line 100 is the local query = DB_RP:query(UPDATE ....) line so it looks like we have progressed...
Seems like a generic error? Why is this one happening?? Thanks for your help :D
Timmy:
Lua has a built-in garbage collector. It frees up memory by purging data that that is no longer needed.
Data associated with a disconnected player will get purged soon after they disconnect. The Player object will become NULL.
Player can be accessed inside PlayerDisconnected hook
--- Code: ---hook.Add("PlayerDisconnected", "Instant", function(ply)
print("Goodbye " .. ply:Nick())
end)
--- End code ---
But not if we introduce a 1 sec. delay
--- Code: ---hook.Add("PlayerDisconnected", "DelayedBroken", function(ply)
timer.Simple(1, function()
print("Goodbye " .. ply:Nick()) -- Tried to use a NULL entity!
end)
end)
--- End code ---
Whether we wait for a 1 sec. timer or for the answer to a database query doesn't matter. The Player object will be gone.
Unless the data is stored in variables before the delay
--- Code: ---hook.Add("PlayerDisconnected", "Delayed", function(ply)
local nick = ply:Nick()
timer.Simple(1, function()
print("Goodbye " .. nick)
end)
end)
--- End code ---
Store the data you need in variables instead. You can still access those after a delay.
Can't I just store the entire Player object in a variable?
Variables can store numbers, strings, booleans and nil values. You can not store objects in variables, only references to those objects.
Variables can only store references to objects
--- Code: ---hook.Add("PlayerDisconnected", "StorePlayer", function(ply)
local dude = ply
timer.Simple(1, function()
print("Goodbye " .. dude:Nick()) -- Tried to use a NULL entity!
end)
end)
--- End code ---
Navigation
[0] Message Index
[#] Next page
Go to full version