How to fix my player added event not firing?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am attempting to solve the issue of studio loading in player before the server scripts run.

  1. What is the issue? Include screenshots / videos if possible!

The player added event in my module script is not firing or printing out anything. It also doesnt work in a real game. I get no errors.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I cant find any discord or topic that shows a fix as my code matches what people suggest to solve this issue.

for i,v in pairs(game.Players:GetPlayers()) do
	coroutine.wrap(playerAdded)(v)
end

game.Players.PlayerAdded:Connect(playerAdded)

game.Players.PlayerRemoving:Connect(function(player)
	if JoinedPlayers[player.UserId] ~= nil then
		JoinedPlayers[player.UserId] = nil
	end
end)
1 Like

You should iterate over existing players and calling your playerAdded function manually, then connect to Players.PlayerAdded

local function playerAdded(player)
     -- ...
end

-- Call our function for existing players, as if they joined right when we connect:
for _, player in pairs(game.Players:GetPlayers()) do
     playerAdded(player)
end
game.Players.PlayerAdded:Connect(playerAdded)

Edit: when asking for scripting help, you should only post relevant parts of your problem. That’s a lot of code to sift through for potential problems, and nobody wants to read it all. Boil down your problem into one, reproducible issue.

1 Like

Will do, That didnt solve the issue though even after I moved my loop before the player added event

1 Like

Just noticed you were already doing this at the end of your code. Since playerAdded yields in some way (player.CharacterAdded:Wait() and data stores, for example), you should use coroutine.wrap(playerAdded)(player) so you can process all existing players at once.

1 Like

Still nothing, I will update my code to what I have now

1 Like

Perhaps to better understand the flow of your code, you should call print with relevant information in various places. For example, before manually calling playerAdded or when you check if JoinedPlayers[player.UserId] ~= nil then in playerAdded.

I dont see what you mean. There is a print statement on the first line of the playerAdded function before any checks are made.

So it turns out the loop of game.Players:GetPlayers() is not looping at all as I put a print statement inside it and its not prining anything still

If that’s not printing, go one level up. You’ve already identified your function isn’t running, so see if the code that calls it isn’t running (spoiler alert: it isn’t, so try using print to find out why). Debugging is challenging in its own right, so be sure to expose as much useful information to yourself as possible.

so as it turns out the whole module isnt even running for some reason even though I requrire it in a seperate server script

Bingo, there’s your problem. Remember that if ModuleScripts require each other (eg A → B → A), the engine will halt indefinitely without warning. This could be your problem.

My script that requires it is not a module though. I put a print statement in the server script but it does not print anything past the require of the module

A little unrelated to the thread but:

for _, player in pairs(game.Players:GetPlayers()) do

I’m getting confused now because people keep using either ipairs or pairs for gathering the players. How do the two differentiate from each other when you’re gathering the players?

I always do the following:

for _, v in ipairs(game.Players:GetChildren()) do
    -- code
end

ipairs is generally used for array-like tables and pairs is generally used for dictionary-like tables.

I tend to use ipairs for things where order matters, and pairs where it doesn’t. Here, order doesn’t matter (when iterating over players, generally I don’t care about the the first for-loop value (i or _ when its not used).

Cool, so that’s been ruled out. Now dig back down into your code to see where it’s yielding indefinitely. print(debug.traceback()) may help here. If you can pinpoint the line that will help immensely.

The issue was just an info module storing all my weapon data that is required in this module. The weapon info module requires 2 more modules which eventually lead back to weapon manager causing a require loop. I should have noticed that. Thanks for your help! :smile: One more question, I see some devs use coolFunction()()instead of passing them as a parameter like you did above and I am wondering if there is any advantage over using the traditional way?

1 Like

Glad you found it out! This little module I wrote emits warnings for requires that take too long, which can help find require cycles like this:

Not sure I understand exactly what you’re talking about with that second bit. Feel free to DM me or link me a new thread if you want to know more about it, since this thread’s purpose is done :wink:

1 Like