My first library.. Need help.

Hello. I am making a clientside library. Here’s my code:



local vx
vx = {}
function vx.roundedbox( w, h )
	draw.RoundedBox(0, 0, 0, w, h, Color(30, 30, 30, 200))
end


I have this file AddCSLuaFile’d and included, and when I go put



vx.roundedbox(50, 50)


in my HUDDraw code, it gives me errors saying attempt to index global ‘vx’ (a nil value)

[editline]27th October 2013[/editline]

Still can’t figure it out :l

remove the “local vx” at the top.

:o idk how that fixed it… but it did… Thank you :smiley:

Yeah, when you call the function it doesn’t know what vx is.

[editline]
se
[/editline]

Because vx was a local table (usable in that file.), but when you remove local it becomes a global table (usable anywhere.)

Okay, so now, it works, but now I want to define a serverside library. The code above was clientside. Here’s my code:



vx = {}
local cmds = {}

function vx.AddChatCmd( cmdname )
	table.insert(cmds, cmdname)
end

function ChatCmds( ply, text, public )
	for _, v in pairs(cmds) do
		if (string.sub(text, 1, string.len(v)) == v) then
			 print("You said " ..v.. "!")
			 return(true)
		end
	end
end
hook.Add( "PlayerSay", "AllCmds", ChatCmds )


I tried running

vx:AddChatCmd("/hi")

and I get “attempt to index global ‘vx’ (a nil value)”

vx.AddChatCmd not vx:AddChatCmd (wrong operator)

I fixed that.



[ERROR] gamemodes/vxlightning/gamemode/plugins/chatcmds/sv_plugin.lua:1: attempt to index global 'vx' (a nil value)
  1. unknown - gamemodes/vxlightning/gamemode/plugins/chatcmds/sv_plugin.lua:1
   2. include - [C]:-1
    3. unknown - gamemodes/vxlightning/gamemode/init.lua:23



sv_plugin.lua:



vx.AddChatCmd("/hi")


Make sure your vx table is declared before you run the function.

Usually this is because one file is included before another.

For some reason, when i put vx.AddChatCmd("/hi") in the library file under function vx.AddChatCmd( cmdname ), it works, I type “/hi” in game, and it prints “You said /hi!”

Hmm…