Help on tables (beginner)

After learning 1% of tables and the fact that you cannot pass functions through RemoteEvents, I realized I had to make my custom DevConsole commands in the ServerScript.
I want them to be formatted something like this:

local Commands = { -- module
	"print something" = { -- the command
		desc = "print stuff", -- the description
		func = function() -- the function to execute when called
			print("yes")
		end,
	}
}
return Commands

And I want to access and run the func, when I want to. But how would I do these?

I’m not quite sure what you’re asking, but you can learn stuff about tables here: Tables | Roblox Creator Documentation

You can also get information from youtube videos but I don’t recommend

I am asking on how I would format the code above, since it’s obviously not correct. I’ve seen this done in the past, I suppose with guns, and I would like to do it too. I would want to know how to access and run the function linked in the table too.

local Commands = { -- module
	["print something"] = {
		desc = "print stuff", -- the description
		func = function() -- the function to execute when called
			print("yes")
		end,
	}
}

Commands["print something"]["desc"]  --This is how you would get Desc
Commands["print something"].func() -- This is how to call func from the table
1 Like
local Commands = { -- module
	["print something"] = { -- the command
		desc = "print stuff", -- the description
		func = function() -- the function to execute when called
			print("yes")
		end,
	}
}
return Commands

You do Commands["print something"].func() to acces it

2 Likes

This might help

1 Like

Interesting…?

Edit: I just saw

if you were to print desc it would print but since your just stating it, it wouldn’t work (it would error), you can also make it into a variable

local desc = Commands["print something"]["desc"]
1 Like
-- Define functions here
local function func1()		
	--Do something
end

local function func2()		
	--Do something
end

-- Put functions in list
local list = {func1, func2}

-- Run function from list here
list[2]() -- This should run function 2
list[1]() -- This should run function 1

That is not the format I wanted, it was listed above. But thanks though!

Alright, well why is the topic set to solved anyways?

Because the right formatting and the way to access the function and description was posted.

How exactly is mine the wrong formatting? With that you can run the functions at any time you want using 1 line

there is a difference between list and dictionaries

There is no trace of the description, and the command.

Oh alright then. I’m still working on tables myself but I thought that would work.