Module "self' help

I have a big problem with the modules. I dont understand how to use self, I’ve looked at a lot of modules, but everything is different there. I need to make this script:

local module={}

local rs=game:GetService'ReplicatedStorage'
local plrs=game:GetService'Players'

local admin={'12345678'}
local ban={'12313242'}



local cmds=	{
	{
		map=function(self)--'map Snow City'
			if typeof(self.arg1)=='string'then
				workspace:SetAttribute(self.cmd,self.arg1)--('map','Snow City')
				return true
			else
				return'Argument 1 must be a string'
			end
			--workspace has attribute 'map'
		end,
		req=0
	},
	{
		kick=function(self)--'kick PlayerName'
			local player=game.Players:FindFirstChild(self.arg1)

			if player~=nil then 
				if player.UserId~=self.plr.UserId then
					if not table.find(admin,player.UserId)then
						player:Kick()
						return true
					else
						return"You can't kick admins"
					end
				else
					return"You can't kick yourself"
				end
			end
		end,
		req=1
	}
}


function module.new(plr)
	local self=setmetatable({
		plr=plr,
		rights=(table.find(admin,plr.UserId)and 2)or 1,
	},module)
	return self
end


function module:write(cmd)
	if not cmd:match' 'or cmd:gsub('%s+','')==''then return end
	
        local split=cmd:split(' ')
	cmd=split[1]
	self.cmd=split[1] or nil
	self.arg1=split[2]or nil
	self.arg2=split[3]or nil

	for _,v in pairs(cmds)do
		if v[cmd]then
			if self.rights>=v.req then
				local func=v[cmd]
				func(self)
				return true
			else
				return'Not enough rights'
			end
		end
	end
	return'Command not found'
end 




return module

I need the module to activate something like this script:

local rs=game:GetService'ReplicatedStorage'
local console=require(rs.Console)

game.Players.PlayerAdded:Connect(function(plr)
	local cmd=console.new(plr)
end)

rs.Events.Console.OnServerEvent:Connect(function(fired,command)
	return console:write(command)
end)

I do not know how it would be better to make a table with commands so as not to write “function(plr,arg1,arg2)” in each one, so I think it should be something like this through self

Maybe the activation of the module can be easier to do, but I do not know at all when and how the modules are activated.

I will be very glad to help, as this is the only thing I need to understand in all scripting

1 Like

self refers to the table which the function was indexed.

1 Like

I don’t think you understand how object orientated programing in Lua works. That’s what seems to be the issue. And it seems like there isn’t a reason to be using this approach for this.

1 Like

I want to understand this. I cant avoid this problem, that’s the only thing I want to understand

When using self inside of functions in a table such as

function module:foo()
    self.something
end

self is just a place holder for the table that called the function. you can do the same thing with different syntax by doing this:

function module.foo(tbl)
    tbl.something
end

let’s test it by doing this:

-- in a module script:
local module = {}
module.value = 10

function module:foo()
    print(self.value)
end

return module

you would call this from a script like so(assuming that you are requiring a module script):

local module = require(module_path)
-- we can now call that same thing two different ways
-- way one:
module:foo() -- prints the value of 10

-- we can also do this:
module.foo(module) -- this does the same thing

you can also try changing the value from the server script and see that it prints the new value.

1 Like

self is a variable for functions that were defined with : in a table/userdata, which is considered Syntax Sugar.

When defining a function with : instead of ., you’re telling the function that the self variable must be used instead of the first argument.

Calling a function with : will use the first argument as the table it was indexed from.

In other words:

local object = setmetatable({},{__tostring="Object"}) -- returns "Object" when directly printed
function object:SelfMethod(arg1)
   -- "self" becomes the object table
   print(arg1,self == object)
end
function object.Method(arg1) 
   -- "self" is not defined, but it gets included as the first argument if it's called with ":"
   print(arg1,self)
end

object:SelfMethod("Hello world!") --> "Hello world!", true
-- Above is equivalent to blow.
object.SelfMethod(object,"Hello world!") --> "Hello world!", true
-- But NOT equivalent with this.
object.SelfMethod("Hello world!") --> nil, false

object:Method("Hello, world") --> Object, nil
object.Method("Hello, world") --> "Hello, world", nil

Outside of this, self is just a variable name with only their text being highlighted.

1 Like

Let’s take an easy example what self actually refers to based on this code. self here refers to cmds, the container itself the function is found within.

Note that functions that are written function foo:bar() has an implicit self. You don’t have to declare it.

1 Like

I solved it. I just did the activation through the script

local mod=require(game.ReplicatedStorage.Console)

local me=mod.new('args')

me:write('command') -- not" "mod:write()"

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