[SOLVED] Trying to make a table with function run

Hello guys, im trying to call a table with functions in a module script, something like this:


Local module = {}

Function module.new(player)

Local newmodule = {}
newmodule = {
["Move"] = {
["Baller"] = function()
--code stuff
End;
};
}

Function newmodule:create(name, move)
newmodule[name][move]()
End

Return newmodule

End

Return module

Gets me an error i dont romember quite well what is but says that is "attempting “name” "

2 Likes

Well, we need the exact error message and where did it happen.

Is the error: “attempting to index nil with name” by any chance?

In the create function ‘newModule’ isn’t defined, but may be accessed via ‘self’.
Look up metatables, as I think that might be helpful in this scenario.

Yes i check and is "attempt to index nil with ‘name’ ".

I try to use meta tables, like you said, but this is so new to me, the metatables idk how to learn or what to do in this case escenario.

If you are trying to implement OOP, you might want to give these a read first:

Although I don’t see why you would need OOP in this case.
A table of functions work just as well here, unless I’m not understanding something.

1 Like

This should run as an example of Oop, using your code, it seems a little overcomplicated although I’m not sure specifically what you want to do with it.

local module = {}
module.__index = module --added 

--this will return a new module to the script that ran module.new(player)
function module.new(player)

    local newmodule = {}
    setmetatable(newmodule, module). --added
    newmodule = {
       ["Move"] = {
          ["Baller"] = function()
             --code stuff
             print("works")
          end
       }
    }
    return newmodule
end

--this will run the code inside "Baller" IF "Baller" is passed as the movetype
function newmodule:create(movetype)
    self.Move[movetype]()
end

return module

--in a script
local myModule = require(pathToModule) --define the path yourself
local myNewModule = myModule.new(player) --get the player yourself
myNewModule:create("Baller")

Is very close that im looking but what i want to is to


Table = {

["Firstability"] = {
["Attack1"] = function()
Print("worm")
End;
};

["Secondability"] = {
["Attack2"] = function()
Print("work")
End;

}
}

Function module:create(name, attack)

--name is the name that is inside of the "Table"
--attack is the function

End

Idk how to make a function to run a table inside a table and runs a function, how???

Try changing the create to this:

function newmodule:create(ability, attack)
    self[ability][attack]()
end

trusth me that i try that but gives the same error that i say in the beggining of the post,
the “attempt to index nil with ‘Standless’”

and i try it to do it agein but gives the error

Make sure name is = “Move” and move is = “Baller”, otherwise you are getting nil, and nil is not a function

i did make sure the names are right, so i guess im getting a nil? idk

image
How does that script even run though…

And functions is written with a Capital, which means it’s a variable? That script makes no sense
image

uhhh here is how is run though, read the post

local module = {}
module.__index = module --added 

--this will return a new module to the script that ran module.new(player)
function module.new(player)

	local newmodule = {}
	setmetatable(newmodule, module). --added
	newmodule = {
		["Standless"] = {
			["Gun"] = function()
				--code stuff
				print("works")
			end
		};
		["Baller"] = {
			["c"] = function()
				--code stuff
				print("works2")
			end
		};
	}
	
	--this will run the code inside "Baller" IF "Baller" is passed as the movetype
	function newmodule:create(name, attack)
		self[name][attack]()
	end
	
	return newmodule
end

return module

Looks like you’re pretty new to programming or you’re using ChatGPT to create code for you.
I highly advise you to start reading the documentation from the very beginning.

For your issue, you can check out how module scripts work here:

aghhh ok ok -__-, ima try to read that, agein

After you’re sure you understand everything and can create decent code, you can give this page a try to start on OOP. (Object Oriented Programming)

i mean im sure i understand module scripts and how works

Then give the above link a try. It explains briefly on how OOP works and how you can create your own classes and objects.

im just think i dont know some little details