• Function Override (Nickname System)
    5 replies, posted
Hello Facepunch again, I was wondering if i could override a player function like ply:Nick()? I understand lua very well but i cant find out how to do it. The reason i want to override it is so all of the custom scripts i have made, use the nick function and i dont want to edit them all just for this change. (It also makes other addons compatible) PS:This dont work [LUA] local plyMeta = FindMetaTable( "Player" ) function plyMeta:Nick() if self:GetNWString("Nickname") == "" then return self:Name() else return self:GetNWString("Nickname") end end [/LUA]
I haven't tested this but in theory it should work: [lua] local plyMeta = FindMetaTable( "Player" ) local old_nick = plyMeta.Nick function plyMeta:Nick() return self:GetNWString( "Nickname", old_nick(self) ) end [/lua]
Thanks it works [editline]10th July 2015[/editline] Now that works other things dont :( my sql query is returning false (means error) but i dont know hot to test for it [LUA] sql.Query( "CREATE TABLE IF NOT EXISTS player_nicknames ( SteamID string, Nickname string )" ) local nick = sql.Query( "SELECT Nickname FROM player_nicknames WHERE SteamID="..ply:SteamID() ) if nick == "" then //somestuff else //add to database end [/LUA] [editline]10th July 2015[/editline] Anyone?
[QUOTE=rtm516;48167128]Thanks it works [editline]10th July 2015[/editline] Now that works other things dont :( my sql query is returning false (means error) but i dont know hot to test for it [LUA] sql.Query( "CREATE TABLE IF NOT EXISTS player_nicknames ( SteamID string, Nickname string )" ) local nick = sql.Query( "SELECT Nickname FROM player_nicknames WHERE SteamID="..ply:SteamID() ) if nick == "" then //somestuff else //add to database end [/LUA] [editline]10th July 2015[/editline] Anyone?[/QUOTE] [LUA] sql.Query( "CREATE TABLE IF NOT EXISTS player_nicknames ( SteamID string, Nickname string )" ) local nick = sql.Query( "SELECT Nickname FROM player_nicknames WHERE SteamID="..sql.SQLStr(ply:SteamID()) ) if nick == "" then //somestuff else //add to database end [/lua] MySQL uses the colon character for prepared statements, therefore you need to escape them. It's also a good idea to wrap your strings as actual strings.
Now the select statement works it gets to this point [LUA]sql.Query( "INSERT INTO player_nicknames VALUES ("..sql.SQLStr(ply:SteamID())..","..sql.SQLStr(ply:Nick())..")" )[/LUA] and never puts anything in the database PS: Tried this aswel [LUA]sql.Query( "INSERT INTO player_nicknames( SteamID, Nickname ) VALUES ( "..sql.SQLStr(ply:SteamID())..", "..sql.SQLStr(ply:Nick()).." )" )[/LUA] [editline]11th July 2015[/editline] Im running this on single player will this affect it? (i know steam id will be STEAM_0:0:0)
[QUOTE=rtm516;48176441]Now the select statement works it gets to this point [LUA]sql.Query( "INSERT INTO player_nicknames VALUES ("..sql.SQLStr(ply:SteamID())..","..sql.SQLStr(ply:Nick())..")" )[/LUA] and never puts anything in the database PS: Tried this aswel [LUA]sql.Query( "INSERT INTO player_nicknames( SteamID, Nickname ) VALUES ( "..sql.SQLStr(ply:SteamID())..", "..sql.SQLStr(ply:Nick()).." )" )[/LUA] [editline]11th July 2015[/editline] Im running this on single player will this affect it? (i know steam id will be STEAM_0:0:0)[/QUOTE] -snip- I'm tired
Sorry, you need to Log In to post a reply to this thread.