Calling functions from table. (Or alternatives)

Hello! I’m currently trying to make a modular core for some doors I’m making.
I’m doing this with the idea of not having to rewrite the door’s code every time I’m making something more special, with custom actions and attributes, etc.

With this, instead of writing a long string of if → thens, I decided to create a table with string values assigned to individual functions.

Currently I am using this; keep in mind I’m new to tables.

Actions = {
	["Open"] = Open();
	["Close"] = Close();
}

InitiateAnim.Event:Connect(function(NewState)
	for _,v in pairs(Actions) do
		if NewState == Actions[v] then
			Actions[NewState] -- Not a thing, that's why I'm posting
			return
		end
	end
end)

Now I don’t even know if I’m properly looking for arguments in the table, but I am unsure how I could fire the assigned function.

Some extra help on how to navigate ‘subarguments’ would be helpful too if you have the time.

In Lua, x[y] is a value and values can’t run alone (unlike Python etc).
As you’re doing Function(), you’re calling the function beforehand, for it to store the function you need to remove the parenthesis.

Actions = {
    ["Open"] = Open;
    ["Close"] = Close;
}

and you can call that function through this:

Actions[NewState]()
2 Likes

Thank you for helping me on that, but I still have another issue I don’t think deserves another post.

Trying to read the first value of the table returns nil what ever the indicator is.

Actions = {
	["Open"] = Open;
	["Close"] = Close;
}

print(Actions[1]) -- returns nil
print(Actions[2]) -- returns nil

script.Parent.InitiateAnim.Event:Connect(function()
	for _,v in pairs(Actions) do
		print(Actions[v]) -- returns nil
	end
end)

In case you can’t / don’t want to respond, I can make another post. But thank you anyway!

You’re just indexing the table and not actually calling the functions. The prints should be printing more than just nil though unless you haven’t actually defined open/close. Also, you shouldn’t be using bindable events for this.