Unknown error in GLua

I have a problem, I have this snip of 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 .. "
")
	end
end )

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:


[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


Incase needed, full 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 .. "   
")
	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("


Reading Tabs.txt ...")
 
textdata = file.Read("Tabs.txt")
print("Tabs.txt is read. Data: 
" .. textdata)
print("")
 
print("Seperating lines ...")
lines = string.Explode("
", 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 .. "
")
	end
end )
print("
")


For the leechers out there, hers mai cud (its pretty crappily made tbh)



file.Append("Tabs.txt", "")
concommand.Add ("permawep_clear", function( ply, cmd, args)
if ply:IsAdmin() then
		print ("Removing all. 
")
		file.Write("Tabs.txt"," ")
	else
		print ("Permission denied. 
")
	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","
".. steamid .. " has ownership of the " .. args[2] .. "
")
	else
		ply:ChatPrint("You are not allowed to do that command, " .. ply:Nick())
	end
end )

concommand.Add( "permawep_check", function( ply, cmd, args )
	print("
Reading Tabs.txt ...")
	
	local textdata = file.Read("Tabs.txt")
	print("Tabs.txt is read. Data: 
" .. textdata)

end )

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

here ju gooo

(Same error, kinda)


[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

Localized ALL variables, even.
ALL OF THEM.

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



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 .. "   
")
	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("


Reading Tabs.txt ...")
 
local textdata = file.Read("Tabs.txt")
print("Tabs.txt is read. Data: 
" .. textdata)
print("")
 
print("Seperating lines ...")
local lines = string.Explode("
", 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 .. "
")
	end
end )
print("
")




permawep_add "STEAMID:DDWIIWIW" "csgo_gay_knif"


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:



// 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 .. "
")
	end
end )


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

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:


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] .. "
")
	else
		ply:ChatPrint("You are not allowed to do that command, " .. ply:Nick())
	end
end )

concommand.Add( "permawep_check", function( ply, cmd, args )
	print("
Reading Tabs.txt ...")
	
	local textdata = file.Read("Tabs.txt")
	print("Tabs.txt is read. Data: 
" .. textdata)
	print("Seperating lines ...")
	
	local lines = string.Explode("
", 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 .. "
")
	end
end )

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

Clear out the text file and start anew. Yes, MySQL is better. Or even parsed JSON.

a new wot?

[editline]28th July 2016[/editline]



] 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





1	=	STEAM:0:0:0:0 csgo_something
2	=


See a problem here?

[editline]28th July 2016[/editline]

Also, no clue why it prints correctly if 2 is clearly nil