function PlayerCore:Initiate()
Players.PlayerAdded:Connect(self.OnPlayerAdded)
end
But the problem with this is that the function OnPlayerAdded gets called as soon as I setup the event, and then when a player does join the event doesn’t get called at all.
Alternatively I tried this:
function PlayerCore:Initiate()
Players.PlayerAdded:Connect(function(Player)
self:OnPlayerAdded(Player)
end)
end
This works but it’s just inefficient, I’d like to make the first method work but I’m not sure exactly how.
I don’t know your exact issue since I don’t follow your explanation well, but it sounds more like you’re incorrectly handling the situation or propagating useless worries for yourself. Keep in mind the difference between each code sample and what they are doing:
First sample calls self.OnPlayerAdded(Player)
Second sample calls self.OnPlayerAdded(self, Player)
In the first case, noting what arguments are passed, you would have to account for self by checking how many arguments are passed and working from there with local variables. Frankly though this is more trouble than it’s worth and in some cases you can’t do it.
I’m not sure how you find the second method inefficient? This seems perfectly fine for your use case. I mean, even the PlayerModule does it this way. If anything this is much more efficient and readable than trying to force interchangeability. Just go with that over the first.
Colon : relates to object-oriented programming styles. Anywhere you see that colon, it’s passing itself as the implicit first argument (self). With dot syntax, it’s a regular call passing all the arguments with nothing hidden. In your first example, you’d have to pass self which you can’t.
For all intents and purposes, if you’re following object-oriented programming styles, you should approach this problem using the second method. It’s less hassle on you to sort out arguments in the function call and you can retain a good style.
As mentioned in my earlier post, the PlayerModule does it the second way. You can also reformat your code so it follows a single line (though it’s not particularly readable):
function PlayerCore:Initiate()
Players.PlayerAdded:Connect(function(Player) self:OnPlayerAdded(Player) end)
end