Having a table function edit table

The title sounds a little bit odd but essentially
I have a table set up like this in a module

return {
	Prompt = {
		Text = "Do a thing",
		Enabled = true,
		Activation = function()
			--Edits the Enabled property
		end
	}
}

The activation function should able to edit the Enabled property but big problem
I don’t know how

You can do this by restructuring the code a little bit (no performance difference outside of the function actually involving working code);

local t = {Text = "Do a thing", Enabled = true}
t.Activation = function(self)
	self.Enabled = not self.Enabled
end

return{Prompt = t}

You can then call Module.Prompt:Activation() (so long as the module has been required) to toggle the enabled index.

1 Like

you can use the colon operator (and using the self parameter) to access the enabled field.

return {
	Prompt = {
		Text = "Do a thing",
		Enabled = true,
		Activation = function(self)
			self.Enabled = false
			print(self.Enabled)
		end
	}
}

-- in some script

Prompt:Activation()
2 Likes