[SOLVED] Trying to make a table with function run

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

what? wdym by that, that just sounded mean or something

Right here, you are replacing the initial table with a new one, which means the old one, alongside the metatable assigned to it is simply ditched.
image
You can put the setmetatable under the second newmodule, and get rid of the first newmodule

With proper OOP syntax, this function should be outside newmodule.new(), and should be a function of module, module:create(). Using self inside that function will reference newmodule.
image

If you are a beginner, I really do not recommend using OOP, as it’s not intuitive to understand in lua and frankly not needed

However, putting functions inside tables is something I like doing, and that can help keep code organized, by avoiding a lot of if statements

If you’re trying to work with metatables and OOP, I suggested that you give the above link a try, sorry if that sounded mean or something, didn’t mean to.
As the person above me said though, it’s frankly not needed and if you’re a beginner you can use other programming methods such as functional programming with modules. It should also fit in your case as well.

1 Like


Here is how it should look like

image
And it works!!

ohhhhhhh, thanks really actually. tbh i know is really complicated to like use metoths like OOP
to do stuff. but im doing stuff that requires some complicated stuff, and i didnt know that i was making a new table, in the beggining of making the script, that table that contains the function was not empty and have the stuff yknow? but anyways thanks, really appreciated

dammit i didnt realice i was firing wrong the RemoteEvent. when @Wigglyaa did help on the problem, was all good all along, and i was firing the RemoteEvent wrong that why gives me the same error in the begging of the post, this was the misstake i did:

--this is on local script
function call()
	move:FireServer(Player,"Standless", "Gun")
end

instead, needs to be like this

--this is on local script
function call()
	move:FireServer("Standless", "Gun")
end

this is because on the server script, by doing game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, a, b)

you already have the player on the OnServerEvent:Connect(Function().

so that why it give me the same error as before.

but i really apriciated the help by @Tomi1231 by refine the module script at the end, and the help
@TheRealANDRO, i know that i need to read more the docs, but i apriciated the reminder <3 and the helpof @Wigglyaa.

so in summary:

Module Script:

local module = {}
module.__index = module

function module.new(player)
	
	local newmodule = {
		["AbilityOne"] = {
			["punch"] = function()
				--code stuff
				print("works")
			end
		};
		["AbilityTwo"] = {
			["Kick"] = function()
				--code stuff
				print("works2")
			end
		};
	}
	
	setmetatable(newmodule, module)
	
	return newmodule
end

function module:create(name, attack)
	self[name][attack]()
end

return module

Server Script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, a, b)
	local myModule = require(game.ServerScriptService.damages.MoveSet)
	
	local myNewModule = myModule.new(player)
	myNewModule:create(a, b)
end)

Local Script:

local move = Game.ReplicatedStorage.RemoteEvent

function call()
	move:FireServer("Standless", "Gun")
end

--something to action the function, like a mouse click or input thing
call()
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.