Sorry I really couldn't think of a better title for this error. So the problem I'm having is the table, the script is supposed to return 2 members but it's saying the 2nd member is nil? It found the first one but not the second.
Image: https://i.imgur.com/zrGPG5C.png
The error has to be in this function (at no point does the forcefield function set the 2nd member nil):
function RunCommand(Str) CMD = nil Func = nil ArgsReq = nil Args = {} -- Get Command and other ****-- Find = string.find(Str, " " ) CMD = string.sub(Str, 1, (Find - 1)) ArgsReq = Commands[CMD].ArgsReq -- Get Args -- if ArgsReq > 0 then for i = 1, ArgsReq do Arg = nil Find2 = string.find(Str, " ") Str = string.sub(Str, (Find2 + 1)) Find3 = string.find(Str, " ") if Find3 then Arg = string.sub(Str, 1, (Find3 - 1)) else Arg = Str end table.insert(Args, Arg) end if ArgsReq > 1 then for i = 1, #Args do print(tostring(Args[i])) end Commands[CMD]:Func(Args) elseif ArgsReq == 1 then Commands[CMD]:Func(Args[1]) end else spawn(Commands[CMD].Func()) -- Ignore this it doesn't get called. end end
The function you have provided cannot be directly at fault since it doesn't touch Table
. If you suspect it's passing incorrect arguments to your forcefield function, you should print out the Args
table (that is, its values) so you can verify whether your string operations are working correctly.
You talk about being unable to use local variables. Each scope/function (including the area of the script outside all functions) has a limit of 200 local variables and 60 up-values each (upvalues are local variables/arguments that are defined outside the function but that the function uses). Therefore, you should be able to use local variables in every function you need. Not to mention, you probably shouldn't be having 200 different variables all in the same scope -- surely some of them could be put into functions or do end
blocks. Changing your script to make sure it can use local variables where it needs is important -- not only are local variables accessed faster, they ensure that you can use the same variable name in different places (for different purposes) without them interfering with each other.
Other notes:
spawn
string.find
does return multiple values, but if you don't use them, lua simply discards them. There are also cases where all but the first returned value is the only one that will be saved, ex: local s = "hi"; print(s:find("hi"), s:find("hi")) -- 1 1 2
function add(a, b) return a+b end print(add(unpack({1,2}))) --3