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 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
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
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
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.
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
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.