Trouble with self

I trying to achieve using self I have a connect of Players.playerAdded:Connect(self.PlayerAdded)
the trouble is that in the self.PlayerAdded function self is not the module but the player.

My question is how can I get self in the self.PlayerAdded function if it is called by PlayerAdded connect?

Here is my code

local Players = game:GetService("Players")

local Module = {}

function Module:PlayerAdded(...)
	print(self, ...)
end

function Module:PlayerRemoving(...)
	print(self, ...)
end

function Module:Initialize()
	Players.PlayerAdded:Connect(self.PlayerAdded)
	Players.PlayerRemoving:Connect(self.PlayerRemoving)
	return
end

return Module

I do not understand why, thanks for the help.

Yes, because self would be nil. I want self to = the Model.

I believe the way you are passing the function unfortunately will not let you do this. You will have to pass self yourself. Maybe a short wrapper could be nice:

local Players = game:GetService("Players")

local Module = {}

local function Closure(f, ...)
	local Args = { ... }
	return function(...)
		return f(table.unpack(Args), ...)
	end
end

function Module.PlayerAdded(self, ...)
	print(self, ...)
end

function Module.PlayerRemoving(self, ...)
	print(self, ...)
end

function Module:Initialize()
	Players.PlayerAdded:Connect(Closure(self.PlayerAdded, Module))
	Players.PlayerRemoving:Connect(Closure(self.PlayerRemoving, Module))
	return
end

return Module

Is there any need to use self in this context? Using the vaiable Module directly would suffice.

1 Like

yes, because self looks nice, I want to use it because it makes me look smarter like I know what I am doing. And because I think it looks nice cool clean other ajectives

1 Like

You cannot call any functions inside a connection and I dont want to use a closure. The closest method I have found is

getfenv(1).self = Module

which I find disgusting

Not sure I understand? The code I sent works.

When I’m calling the Closure function it returns a function and that function will be passed to :Connect

image

even if it did work it would not exactly be what I would’ve preffered.

Works on my machine. But yeah this is not preferred. I think you’re left to reference Module. Anything else you do will look rather scathy.
imageimage

I am using all the beta features and my serverside script is:

local MS= require(script.MS)
MS:Initialize()

Using something because it looks nice? Eh. Not always the best method.

You can very much look like you know what you are doing by not using self here. You’re trying to use it in a context that doesn’t really ask for it, and you’re very much overcomplicating it.

Again, just because it looks nice doesn’t mean it’s the best method.

Just do it without self, you’re making it extremely overcomplicated for no reason:

local Players = game:GetService("Players")

local Module = {}

function Module.PlayerAdded(...)
    print(...)
end

function Module.PlayerRemoving(...)
    print(...)
end

function Module.Initialize()
    Players.PlayerAdded:Connect(Module.PlayerAdded)
    Players.PlayerAdded:Connect(Module.PlayerRemoving)
end

return Module
1 Like

Mine is require(script.Parent):Initialize()

Not sure why yours errors but mine doesn’t.

I can think of two “solutions” to your "I MUST use self" dilemma, but they are quite vexatious to look at.

thats the same thing, just without a var.

fine line you are walking there. throwing shade on me and how I like doing things. think this might be off topic since I asked for a solution not an alternative.

It doesn’t matter.

This is incorrect use of : and self.

You only use self when using objects which you can create using __index.

Using modules and self completely incorrectly does not make you look smart

This topic is a joke. Don’t come asking for an answer and then not accepting it because “it doesn’t look pretty”

2 Likes

I will use Module in PlayerAdded and Removing but for initialize I am still going to use self because it works and it looks nice and it has no effect on performance

1 Like

I appreciate the brutal honesty, despite me being completely against what you want to do, I’m no man to criticize someone else’s freedom.

You can use lambda functions to still use methods inside of the Connect method itself.

local Players = game:GetService("Players")

local Module = {}

function Module:PlayerAdded(...)
    return function()
        print(self)
    end
end

function Module:PlayerRemoving(...)
    return function()
        print(self)
    end
end

function Module:Initialize()
    Players.PlayerAdded:Connect(self:PlayerAdded())
    Players.PlayerRemoving:Connect(self:PlayerRemoving())
    return
end

return Module

If this isn’t quite exactly what you want to do, let me know. That’s what I understood from your question.