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

How would I make it so that in a table it has a table as a value?

So stated above is what i want to know here is the code i want to use this for

local Cmds = 
{
[{"respawn","re"}] = 
	{
	["Run"] = function(Player)
	print("Respawned: ".. Player.Name)
	end,
	["Description"] = "Respawns Player",
	["Args"] = {1,"player"}
	}
}

This is for a admin command command list. Any Help Appreciated

1 answer

✓ Accepted answer

I wouldn't recommend structuring your data quite like that for a situation like this. Mostly for readability and cleanliness of code, but also because it's just not practical.

For starters, constructing a new table with no reference as a key in a key/value pair is sort of useless -- since you have no reference to the key to retrieve the value. Tables in Lua are considered objects, therefore indexing Cmd with a different array of {"respawn", "re"} will result in a logic error (that key will not exist in the table).

Recommendation

Instead, I would recommend structuring your command data like so:

local commands = {
	{{"respawn", "re"}, function(self, ...)
		-- command logic
	end)},

	{{"kill", "k"}, function(self, ...)
		-- command logic
	end)}
}

(where self refers to the player executing the command, and ... represents the command arguments)

This way, you have the consistency of any entry in the command table following a command_alts = command[1]; run_command = command[2] format. You also have the added bonus and convenience of maintaining an array structure to your table, which usually makes development life easier.

Specificity

I did notice, however, that you've added some additional information about each command in your table. To accommodate this, I've made some adjustments:

local commands = {
	{
		alts = {"respawn", "re"},
		description = "reloads the character model", 

		run = function(self, ...)
			-- command logic
		end,
	}
}

...and follow a pattern a bit like that. We're basically just keeping an array of dictionaries; each dictionary being the data of a new command.

Hope this helps, let me know if you have any questions.

3 comments

0

How would i call the command would i do commands[cmd] or what?

0

Nvm found out how thanks a bunch

0

Also how would i make a command with more than 1 argument like a tp command

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