PlayerAdded doesn't fire inside return function

Inside of a ModuleScript, I have return function. Everything else inside of the function works, printing prints, but the PlayerAdded event or the Chatted event doesn’t.

return function(arguments, here)
	print('qwerty')
	playerApi.onJoin(function(player)
		print(player.Name.." joined!")
		local KALVL = Instance.new("IntValue", player)
		KALVL.Value = playerApi.checkPlayerRank(player, configuration)

		executeCommandEvent.OnServerEvent:Connect(executeCommand)
	end)

	playerApi.onChatted(function(player, msg)
		executeCommand(player, msg)
	end)
	print('uiop')
end

After doing some tests, I’ve found that “qwerty” and “uiop” print before the module is loaded.

The playerApi ModuleScript:

function player.onJoin(f)
	game.Players.PlayerAdded:Connect(f)
end

function player.onChatted(f)
	game.Players.PlayerAdded:Connect(function(plr) plr.Chatted:Connect(function(msg) f(player, msg) end) end) 
end

Wouldn’t it be better if you returned the Connections inside the playerApi instead of just declaring them?

1 Like

What do you mean by that?


You just declared the Connections inside the playerApi without returning them. Instead of just doing PlayerAdded:Connect(f) you would need to return that Connection. The same goes for other Connections. Also, Why are you connecting the Events specifically after returning that function? This might be the reason of why it isn’t working as you need to require the ModuleScript which the function is on and then call the function. (Which would take a very small time and because of that delay it would connect the event a little bit after the Player Joins)

local Players = game:GetService("Players")
local PlayerAdded = Players.PlayerAdded
local PlayerRemoving = Players.PlayerRemoving

local PlayerAPI = {}

function PlayerAPI.OnJoin(Callback: (Player: Player) -> ... any?)
	return PlayerAdded:Connect(Callback)
end

function PlayerAPI.OnChatted(Callback: (Player: Player, Message: string, Recipient: Player) -> ... any?)
	return PlayerAdded:Connect(function(Player: Player)
		Player.Chatted:Connect(function(Message: string, Recipient: Player)
			Callback(Player, Message, Recipient)
		end)
	end)
end

function PlayerAPI.OnChattedForPlayer(Player: Player, Callback: (Player: Player, Message: string, Recipient: Player) -> ... any?)
	return Player.Chatted:Connect(function(Message: string, Recipient: Player)
		Callback(Player, Message, Recipient)
	end)
end

return PlayerAPI

I’ve simplified what you’ve put, since type checking hurts my brain. I have this now, but it still doesn’t work.

function player.onJoin(cb)
	return players.PlayerAdded:Connect(cb)
end

function player.onChatted(cb)
	return players.PlayerAdded:Connect(function(plr)
		plr.Chatted:Connect(function(msg, r)
			cb(plr, msg)
		end)
	end)
end

For the delay thing, I’ve also suspected that might be an issue. How could I solve that?

The best thing you can do is to Connect the Events outside the Module. Either inside a Script/LocalScript and store the Connection.

I can’t do that, as the return function passes in values that I need to run the rest of the code.