[SOLVED] Trying to make a table with function run

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.