String turns into Table

Got a problem here that I can’t work out…

I send a function a string and it comes out as a table. – It used to come out as a table. type(nick) is now saying it’s a string, however it says ( a table value ) in the error.

SGNAdmin.IsPlayer(“Semajnad”) is ran which calls SGNAdmin.GetObjectFromNick() - “Semajnad” comes from a chat command.

Output:



The varaiable var in IsPlayer is: semajnad
The type of varaible in IsPlayer is : string
The nickname in GetObjectFromNick is : semajnad
The type of variable for nick in GetObjectFromNick is : string
The argument was correct format.

[ERROR] addons/sgn_admin/lua/autorun/server/init.lua:39: attempt to concatenate local 'nick' (a table value) -- See below for what line 39 is:
  1. GetObjectFromNick - addons/sgn_admin/lua/autorun/server/init.lua:39
   2. func - addons/sgn_admin/lua/autorun/server/init.lua:58
    3. v - addons/sgn_admin/lua/autorun/server/init.lua:352
     4. unknown - lua/includes/modules/hook.lua:84


[ERROR] addons/sgn_admin/lua/autorun/server/init.lua:39: attempt to concatenate local 'nick' (a table value)
  1. GetObjectFromNick - addons/sgn_admin/lua/autorun/server/init.lua:39
   2. func - addons/sgn_admin/lua/autorun/server/init.lua:58
    3. v - addons/sgn_admin/lua/autorun/server/init.lua:352
     4. unknown - lua/includes/modules/hook.lua:84

Semajnad: !freeze Semajnad



print("The nickname in GetObjectFromNick is : " … nick ) - This line is 39 in GetObjectFromNick below



function SGNAdmin.GetObjectFromNick( nick )
	print("The nickname in GetObjectFromNick is : " .. nick )
	print("The type of variable for nick in GetObjectFromNick is : " .. type(nick) )
	for _, ply in pairs(player.GetAll()) do
		if string.lower(ply:Nick()) == string.lower(nick) then
			return ply
		end
	end
	return false
end

function SGNAdmin.IsPlayer( var )
	print("The varaiable var in IsPlayer is: " .. var )
	print("The type of varaible in IsPlayer is : " .. type(var))
	if SGNAdmin.GetObjectFromNick( var ) then
		return true
	end
end



If anyone can give me a hand I’d appreciate it :slight_smile:

EDIT: If I remove the two debug lines in GetObjectFromNick- so it doesn’t fail on those - I get the below:




[ERROR] addons/sgn_admin/lua/autorun/server/init.lua:42: bad argument #1 to 'lower' (string expected, got table)
  1. lower - [C]:-1
   2. GetObjectFromNick - addons/sgn_admin/lua/autorun/server/init.lua:42
    3. func - addons/sgn_admin/lua/autorun/server/init.lua:58
     4. v - addons/sgn_admin/lua/autorun/server/init.lua:352
      5. unknown - lua/includes/modules/hook.lua:84


[ERROR] addons/sgn_admin/lua/autorun/server/init.lua:42: bad argument #1 to 'lower' (string expected, got table)
  1. lower - [C]:-1
   2. GetObjectFromNick - addons/sgn_admin/lua/autorun/server/init.lua:42
    3. func - addons/sgn_admin/lua/autorun/server/init.lua:58
     4. v - addons/sgn_admin/lua/autorun/server/init.lua:352
      5. unknown - lua/includes/modules/hook.lua:84



Line 42 being: if string.lower(ply:Nick()) == string.lower(nick) then

EDIT AGAIN: If I run lua_run SGNAdmin.IsPlayer(“Semajnad”) – I get no errors, so the above two functions obviously work… so why is it mentioning a table?

function SGNAdmin.IsPlayer( var )
print("Variable type is: " … type(


nick

)) // This prints string
if SGNAdmin.GetObjectFromNick( var ) then

Hey Lexic, thanks for that, that was a typo on my part but unfortunately not part of the problem. I rushed typed the post above. I’ll correct it now.

EDIT: I’ve hopefully made the main post a bit clearer now with neater outputs.

-snip, super late-

Hello, as mentioned above I have changed that. I don’t know why it was wrong but it wasn’t the cause of the error. I’ve edited the main post to reflect the proper code.

-Snip, late too- :wink:

It sounds like however you are parsing your chat commands, you are parsing the exploded version of the person’s chat message. Did you forget to reference an actual index in the table of arguments for your chat commands?
i.e.



string.lower( argumenttable )

rather than


string.lower( argumenttable[2] ) -- or whichever you have the index at

No, I do the check to see what variable type it is and it says a string. Plus I know I’m sending it a string and not a table.

[editline]29th August 2015[/editline]

To add to this:

I just hardcoded in a string:



function SGNAdmin.IsPlayer( var )
	print("The varaiable var in IsPlayer is: " .. var )
	print("The type of varaible in IsPlayer is : " .. type(var))
	if SGNAdmin.GetObjectFromNick( "Semajnad" ) then
		return true
	end
end


And it still says string expected, got table.

[editline]29th August 2015[/editline]

**EDIT: Found answer:
**

Not really an explanation, so if anyone knows why then feel free to let me know :slight_smile:

It appears it’s this line that was causing it ( for some strange reason )



if string.lower(ply:Nick()) == string.lower(nick) then


The second string.lower somehow was complaining that nick was a table and not a string. Even though it was a string.

As I lower the chat command earlier, I don’t need to lower the string again anyway.

[editline]29th August 2015[/editline]

EDIT AGAIN:

The only issue with this, is if I want to use SGNAdmin.GetObjectFromNick without going through the chat command, I will have to remember to lower the nick, otherwise they won’t match up.

Anyway, thanks!