• Unknown error in GLua
    25 replies, posted
I have a problem, I have this snip of code: [CODE]// Now let's loop through all the lines so we can split those too for i, line in ipairs(lines) do // line is the current line, and i is the current line number data = string.Explode(" ", line) --playername = data[1].GetBySteamID() playersteam = data[1] playerweapon = data[2] print(playersteam .. " owns this weapon : " .. playerweapon .. "\n") end end )[/CODE] All works, when it runs, it works per usual... except for an error. It gets data from a text, explodes and puts them in vars. (EG playersteam = data[1] and playerweapon = [data2] ) It does its job well, however I have: [CODE][ERROR] lua/test.lua:34: attempt to concatenate global 'playerweapon' (a nil value) 1. unknown - lua/test.lua:34 2. unknown - lua/includes/modules/concommand.lua:54 [/CODE] [IMG]http://ih0.redbubble.net/image.208972181.8643/mug,standard,x400,center-bg,ffffff.u2.jpg[/IMG] Incase needed, full code: [CODE] concommand.Add( 'permawep_add', function( ply, cmd, args ) steamid=table.concat(args," ") if ply:IsAdmin() then print (steamid .. " has been added") file.Append("Tabs.txt" , steamid .. " \n") else name = ply:Nick() ply:ChatPrint("You are not allowed to do that command, " .. name); end end ) concommand.Add ( 'permawep_check', function (ply, cmd, args ) print("\n\n\nReading Tabs.txt ...") textdata = file.Read("Tabs.txt") print("Tabs.txt is read. Data: \n" .. textdata) print("") print("Seperating lines ...") lines = string.Explode("\n", textdata) print("Lines split. The lines are:") PrintTable( lines ) // Now let's loop through all the lines so we can split those too for i, line in ipairs(lines) do // line is the current line, and i is the current line number data = string.Explode(" ", line) --playername = data[1].GetBySteamID() playersteam = data[1] playerweapon = data[2] print(playersteam .. " owns this weapon : " .. playerweapon .. "\n") end end ) print("\n") [/CODE] For the leechers out there, hers mai cud (its pretty crappily made tbh) [CODE] file.Append("Tabs.txt", "") concommand.Add ("permawep_clear", function( ply, cmd, args) if ply:IsAdmin() then print ("Removing all. \n") file.Write("Tabs.txt"," ") else print ("Permission denied. \n") end end ) concommand.Add( "permawep_add", function( ply, cmd, args ) local steamid = args[1] if ply:IsAdmin() then print (steamid .. " has been added") file.Append("Tabs.txt","\n".. steamid .. " has ownership of the " .. args[2] .. "\n") else ply:ChatPrint("You are not allowed to do that command, " .. ply:Nick()) end end ) concommand.Add( "permawep_check", function( ply, cmd, args ) print("\nReading Tabs.txt ...") local textdata = file.Read("Tabs.txt") print("Tabs.txt is read. Data: \n" .. textdata) end )[/CODE]
Use local variables then come back
You have a lot of random globals. Localise your variables and make sure data[2] exists first.
Print data[2], it's probably nothing for some reason
[QUOTE=code_gs;50788195]You have a lot of random globals. Localise your variables and make sure data[2] exists first.[/QUOTE] here ju gooo (Same error, kinda) [CODE][ERROR] lua/test.lua:34: attempt to concatenate local 'playerweapon' (a nil value) 1. unknown - lua/test.lua:34 2. unknown - lua/includes/modules/concommand.lua:54[/CODE] Localized ALL variables, even. ALL OF THEM.
[QUOTE=ZombieKingAAA;50788211]Localized ALL variables, even. ALL OF THEM.[/QUOTE] But did you check the variable isn't nil?
And, the data[2] exists. permawep_add "STEAMID:DDWIIWIW" "csgo_gay_knif"
Post your new code
K BB [CODE] concommand.Add( 'permawep_add', function( ply, cmd, args ) local steamid=table.concat(args," ") if ply:IsAdmin() then print (steamid .. " has been added") file.Append("Tabs.txt" , steamid .. " \n") else local name = ply:Nick() ply:ChatPrint("You are not allowed to do that command, " .. name); end end ) concommand.Add ( 'permawep_check', function (ply, cmd, args ) print("\n\n\nReading Tabs.txt ...") local textdata = file.Read("Tabs.txt") print("Tabs.txt is read. Data: \n" .. textdata) print("") print("Seperating lines ...") local lines = string.Explode("\n", textdata) print("Lines split. The lines are:") PrintTable( lines ) // Now let's loop through all the lines so we can split those too for i, line in ipairs(lines) do // line is the current line, and i is the current line number data = string.Explode(" ", line) --playername = data[1].GetBySteamID() local playersteam = data[1] local playerweapon = data[2] print(playersteam .. " owns this weapon : " .. playerweapon .. "\n") end end ) print("\n") [/CODE]
[CODE] permawep_add "STEAMID:DDWIIWIW" "csgo_gay_knif" [/CODE] But with that you never even used data[1] or data[2] with the above thing? You just used two strings? Data[2] might not be csgo_gay_knif, it might be nil. Try printing it like this: [CODE] // Now let's loop through all the lines so we can split those too for i, line in ipairs(lines) do // line is the current line, and i is the current line number data = string.Explode(" ", line) --playername = data[1].GetBySteamID() local playersteam = data[1] local playerweapon = data[2] print(data[2]) -- actually check properly that it isn't nil print(playersteam .. " owns this weapon : " .. playerweapon .. "\n") end end ) [/CODE]
[QUOTE=MPan1;50788237][CODE] permawep_add "STEAMID:DDWIIWIW" "csgo_gay_knif" [/CODE] But with that you never even used data[1] or data[2] with the above thing? You just used two strings? Data[2] might not be csgo_gay_knif, it might be nil. Try printing it like this: [CODE] // Now let's loop through all the lines so we can split those too for i, line in ipairs(lines) do // line is the current line, and i is the current line number data = string.Explode(" ", line) --playername = data[1].GetBySteamID() local playersteam = data[1] local playerweapon = data[2] print(data[2]) -- actually check properly that it isn't nil print(playersteam .. " owns this weapon : " .. playerweapon .. "\n") end end ) [/CODE][/QUOTE] wtf y is it nil wtf is twis goging GONN (Why is it nil?) And, maybe i should use a diff delimiter than space. Somehow, in the output, I still have the desired effect: STEAM:0:0:0 owns this weapon : csgo_some_gayknife
Yeah, maybe, but try printing 'line' just to see what it looks like before you string.Explode it [editline]28th July 2016[/editline] Wait, so the output actually prints now? That's odd
[QUOTE=MPan1;50788252]Yeah, maybe, but try printing 'line' just to see what it looks like before you string.Explode it [editline]28th July 2016[/editline] Wait, so the output actually prints now? That's odd[/QUOTE] Im about to blow you(r mind) ITS ALWAYS BEEN DOING THAT (With the error, ofc)
Because one of your lines is not exploding into a table of length 2
*Censored for good reason* 6 = STEAM:0:0:0 csgo_some_gaykife
I think I got it. This is still a really bad way to store user info: [code]concommand.Add( "permawep_add", function( ply, cmd, args ) local steamid = args[1] if ply:IsAdmin() then print (steamid .. " has been added") file.Append("Tabs.txt", steamid .. " " .. args[2] .. "\n") else ply:ChatPrint("You are not allowed to do that command, " .. ply:Nick()) end end ) concommand.Add( "permawep_check", function( ply, cmd, args ) print("\nReading Tabs.txt ...") local textdata = file.Read("Tabs.txt") print("Tabs.txt is read. Data: \n" .. textdata) print("Seperating lines ...") local lines = string.Explode("\n", textdata) print("Lines split. The lines are:") PrintTable( lines ) // Now let's loop through all the lines so we can split those too for i, line in ipairs(lines) do // line is the current line, and i is the current line number local data = string.Explode(" ", line) local playersteam = data[1] local playerweapon = data[2] print(playersteam .. " owns this weapon : " .. playerweapon .. "\n") end end )[/code]
[QUOTE=code_gs;50788271]I think I got it. This is still a really bad way to store user info: [code]concommand.Add( "permawep_add", function( ply, cmd, args ) local steamid = args[1] if ply:IsAdmin() then print (steamid .. " has been added") file.Append("Tabs.txt", steamid .. " " .. args[2] .. "\n") else ply:ChatPrint("You are not allowed to do that command, " .. ply:Nick()) end end ) concommand.Add( "permawep_check", function( ply, cmd, args ) print("\nReading Tabs.txt ...") local textdata = file.Read("Tabs.txt") print("Tabs.txt is read. Data: \n" .. textdata) print("Seperating lines ...") local lines = string.Explode("\n", textdata) print("Lines split. The lines are:") PrintTable( lines ) // Now let's loop through all the lines so we can split those too for i, line in ipairs(lines) do // line is the current line, and i is the current line number local data = string.Explode(" ", line) local playersteam = data[1] local playerweapon = data[2] print(playersteam .. " owns this weapon : " .. playerweapon .. "\n") end end )[/code][/QUOTE] MySQL, instead? (Its serverside, too but whatevrr) NIEN got [ERROR] lua/test.lua:30: attempt to concatenate local 'playerweapon' (a nil value) 1. unknown - lua/test.lua:30 2. unknown - lua/includes/modules/concommand.lua:54
[QUOTE=ZombieKingAAA;50788277]MySQL, instead? (Its serverside, too but whatevrr) NIEN got [ERROR] lua/test.lua:30: attempt to concatenate local 'playerweapon' (a nil value) 1. unknown - lua/test.lua:30 2. unknown - lua/includes/modules/concommand.lua:54[/QUOTE] Clear out the text file and start anew. Yes, MySQL is better. Or even parsed JSON.
[QUOTE=code_gs;50788289]Clear out the text file and start anew. Yes, MySQL is better. Or even parsed JSON.[/QUOTE] a new wot? [editline]28th July 2016[/editline] [CODE] ] permawep_add "STEAM:0:0:0:0" csgo_something STEAM:0:0:0:0 has been added ] permawep_check Reading Tabs.txt ... Tabs.txt is read. Data: STEAM:0:0:0:0 csgo_something Seperating lines ... Lines split. The lines are: 1 = STEAM:0:0:0:0 csgo_something 2 = STEAM:0:0:0:0 owns this weapon : csgo_something [ERROR] lua/test.lua:30: attempt to concatenate local 'playerweapon' (a nil value) 1. unknown - lua/test.lua:30 2. unknown - lua/includes/modules/concommand.lua:54 [/CODE]
[CODE] 1 = STEAM:0:0:0:0 csgo_something 2 = [/CODE] See a problem here? [editline]28th July 2016[/editline] Also, no clue why it prints correctly if 2 is clearly nil
[QUOTE=MPan1;50788316][CODE] 1 = STEAM:0:0:0:0 csgo_something 2 = [/CODE] See a problem here? [editline]28th July 2016[/editline] Also, no clue why it prints correctly if 2 is clearly nil[/QUOTE] It's because of the newlines. That's why storing things this way is unreliable.
[QUOTE=code_gs;50788320]It's because of the newlines. That's why storing things this way is unreliable.[/QUOTE] Oh, so if i put a newline between the "STEAMID" and "WEAPON", will i get gud rezults? ur wel i be bad [editline]28th July 2016[/editline] Found [url]https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexa7b4.html[/url]. I'll try something related [editline]28th July 2016[/editline] i r tired i gun go 2 bed pls test 4 me pls b
It doesn't error on all of the lines, only the last line because it is empty, which is a result of writing those newlines at the end of each part. The simple solution is to make the loop ignore the last line. The smarter solution is to validate data[1] and data[2], which will make it work even if an empty line appears in the middle. [lua]if data[1] and data[2] then -- print the message/handle whatever it is you do end[/lua] You could even check if weapons.Get(data[2]) is a weapon as well.
[QUOTE=Zet0r;50790353]It doesn't error on all of the lines, only the last line because it is empty, which is a result of writing those newlines at the end of each part. The simple solution is to make the loop ignore the last line. The smarter solution is to validate data[1] and data[2], which will make it work even if an empty line appears in the middle. [lua]if data[1] and data[2] then -- print the message/handle whatever it is you do end[/lua] You could even check if weapons.Get(data[2]) is a weapon as well.[/QUOTE] It would work for strings, yes? Also, im gonna take an easy way out and just remove the added \n, and add it before. And, changing delimiter for the explode to ; instead of (space)
Fucking hell, just use sql.
Also added a permawep_clear addon, ill post the code in a jiz [editline]28th July 2016[/editline] [QUOTE=man with hat;50791799]Fucking hell, just use sql.[/QUOTE] no [editline]28th July 2016[/editline] Also, how can I make my loop ignore empty new lines? [editline]28th July 2016[/editline] If I can't find a solution, ill find a way to just print every line. That would be easier tbh, since the command permawep_add makes a new line, and i can make it say args[1] .. " owns " ..args[2] or something. [editline]28th July 2016[/editline] Done, here jew gu [CODE] file.Append("Tabs.txt", "") concommand.Add ("permawep_clear", function( ply, cmd, args) if ply:IsAdmin() then print ("Removing all. \n") file.Write("Tabs.txt"," ") else print ("Permission denied. \n") end end ) concommand.Add( "permawep_add", function( ply, cmd, args ) local steamid = args[1] if ply:IsAdmin() then print (steamid .. " has been added") file.Append("Tabs.txt","\n".. steamid .. " has ownership of the " .. args[2] .. "\n") else ply:ChatPrint("You are not allowed to do that command, " .. ply:Nick()) end end ) concommand.Add( "permawep_check", function( ply, cmd, args ) print("\nReading Tabs.txt ...") local textdata = file.Read("Tabs.txt") print("Tabs.txt is read. Data: \n" .. textdata) end )[/CODE] (Marked as solved)
Sorry, you need to Log In to post a reply to this thread.