Tables to Variables

  1. I want to make a script that will split a table into multiple variables. Example: {[1]aa,[2]word,[3]another word} into mutliple variables called arg1, arg2, arg3… It has to work with infinite stuff

  2. The issue is it doesnt work it just spams nil on me and sometimes a word idk why.

  3. Trying random stuff if it maybye works. But it didnt

	function FireCommand(fullcmd)
		local params = {}
		for word in fullcmd:gmatch("%S+") do table.insert(params, word) end
		while wait(0.05) do
			local count = 1 
			if params[count] == nil then break else
				count = count + 1
				print(params[count])
			end
		end	
	end

If I understand what you’re trying to do, table.unpack is what you’re looking for.

For example:

local tbl = {"aa", "word", "another word"}
local aa, word, anotherWord = table.unpack(tbl)

print(aa, word, anotherWord)
1 Like

I think that would work let me try it

Any way you can make it not say all the nil? Im trying to make a command system

Just remove nil manually by first looping in pairs (so you can continue if nil is encountered) and inserting non-nil elements into a new table, then store each element of the new table as a variable.

You could also try storing each element in a dictionary, associated with a key (essentially the ‘variable’) by doing this

 local keys = {}
 for i = 1, #array do
 keys["var"..i] = array[i]
 end

this will of course only work with arrays.

Alright thanks everyone. Awesome response time as always