Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Strange problem with tables, script returning nil?

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

7 comments

0

local variables please

0

I don't understand that the CMD variable is supposed to be set to be.

0

Also, string.find returns 2 values, one is the start of the word and one is the end.

0

@hiimgoodpack, 1) Can't use local variables I reached the max limit. 2) CMD variable is used to get the function data out of a table. Basically creating a universal admin commands if someone ever wanted to add their own commands they can very easily with this function. 3) No. string.find is returning 1 value which is why it printed 2 arguments in the console. See the image link for more info

0

Max limit for local variables?

0

"string.find[edit] string.find (s, pattern [, init = 1 [, plain = false]]) Looks for the first match of pattern in string s. If a match is found, then string.find returns the locations (1, 3, etc.) of s where this occurrence starts and ends; otherwise, it returns nil." Yes it does return 2 values.

0

Can you give us an example of what would be passed as the str?

1 answer

✓ Accepted answer

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:

  • You shouldn't be calling the function that you pass to spawn
  • In lua, 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
  • Setting Arg to nil right before inevitable assignment is unnecessary
  • You can unpack a list of arguments so that they are sent like you would expect, ex:
function add(a, b) return a+b end
print(add(unpack({1,2}))) --3

1 comment

0

Huh I never knew you can use unpack like that. Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.