PlayerAdded not firing

I’ve made a package for equipping morphs and I made a script for it and when I add print(plr.Name) under the PlayerAdded function, it doesn’t print anything. But it does print before the function.

Script:

local Players, Teams, ServerStorage = game:GetService("Players"), game:GetService("Teams"), game:GetService("ServerStorage")
local MorphService = require(1234567890)

local MorphFolder = ServerStorage.Morphs

local Morphs = {
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 235, Morph = MorphFolder.Volunteer},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 237, Morph = MorphFolder.Private},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 238, Morph = MorphFolder["Lance Corporal"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 239, Morph = MorphFolder.Corporal},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 240, Morph = MorphFolder["Sergeant/Staff Sergeant"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 241, Morph = MorphFolder["Sergeant/Staff Sergeant"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 242, Morph = MorphFolder["Sergeant Major"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 243, Morph = MorphFolder.Ensign},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 245, Morph = MorphFolder["Lieutenant/Captain"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 246, Morph = MorphFolder["Lieutenant/Captain"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 247, Morph = MorphFolder.Major},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 248, Morph = MorphFolder["Lieutenant Colonel"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 249, Morph = MorphFolder.Colonel},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 251, Morph = MorphFolder.Brigadier},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 252, Morph = MorphFolder["Major General"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 253, Morph = MorphFolder["Lieutenant General"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 254, Morph = MorphFolder.General},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 255, Morph = MorphFolder.General}
}


-- prints work here
Players.PlayerAdded:Connect(function(plr)
	-- prints dont work here
	plr.CharacterAdded:Connect(function(char)
		for _, morph in pairs(Morphs) do
			if plr.Team == morph.Team and plr:GetRankInGroup(morph.GroupId) == morph.RankId then
				MorphService:EquipMorph(char, morph.Morph)
			end
		end
	end)
end)

I would like to note that the player’s character is reloaded 2 times when the player joins to auto team and handle gamepass tools.

1 Like

Hmm, not sure but is it a possibility that the player is added before the event?

1 Like

PlayerAdded doesn’t work properly in Studio, so you’re going to have create a function for it, connect PlayerAdded to it, and at the end of the script, loop through each player with Players:GetPlayers(), and call the function within each iteration with the player as the parameter.

1 Like

Example, if you did like this:

local Players = game:GetService("Players")
wait(20)
Players.PlayerAdded:Connect(function(plr)
       print("Hi")
end)

The player would be added before the event starts detecting for players added

2 Likes

Tried it and it doesn’t print the message

1 Like

I found a working script for it.

for _, plr in pairs(Players:GetPlayers()) do
	plr.CharacterAdded:Connect(function(char)
		task.wait(5)
		for _, morph in pairs(Morphs) do
			if plr.Team == morph.Team and plr:GetRankInGroup(morph.GroupId) == morph.RankId then
				MorphService:EquipMorph(char, morph.Morph)
			end
		end
	end)
end
2 Likes

This will only work once in Studio, and never in an actual server. You need to make what’s within the loop into a function and call it inside the loop, but also connect it to PlayerAdded.

2 Likes

The issue is you aren’t letting the PlayerAdded be the very first thing that runs since scripts are initialized before players are added. What’s causing it to mess up is you are requiring an external module which will take who knows how long to initialize, thus yielding the rest of the script until it finally loads. You’ll want to make sure PlayerAdded is prioritized above all other yielding parts of the script. You’ll want to change your script into this.

local Players, Teams, ServerStorage = game:GetService("Players"), game:GetService("Teams"), game:GetService("ServerStorage")
local MorphService, MorphFolder
--This bypasses the script initial yielding while still letting this load in the background
coroutine.wrap(function() 
	MorphService = require(1234567890)
	MorphFolder = ServerStorage.Morphs
end)()

local Morphs = {
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 235, Morph = MorphFolder.Volunteer},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 237, Morph = MorphFolder.Private},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 238, Morph = MorphFolder["Lance Corporal"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 239, Morph = MorphFolder.Corporal},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 240, Morph = MorphFolder["Sergeant/Staff Sergeant"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 241, Morph = MorphFolder["Sergeant/Staff Sergeant"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 242, Morph = MorphFolder["Sergeant Major"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 243, Morph = MorphFolder.Ensign},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 245, Morph = MorphFolder["Lieutenant/Captain"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 246, Morph = MorphFolder["Lieutenant/Captain"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 247, Morph = MorphFolder.Major},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 248, Morph = MorphFolder["Lieutenant Colonel"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 249, Morph = MorphFolder.Colonel},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 251, Morph = MorphFolder.Brigadier},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 252, Morph = MorphFolder["Major General"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 253, Morph = MorphFolder["Lieutenant General"]},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 254, Morph = MorphFolder.General},
	{Team = Teams["British Army"], GroupId = 13313660, RankId = 255, Morph = MorphFolder.General}
}

-- prints work here
Players.PlayerAdded:Connect(function(plr)
	-- prints dont work here
	plr.CharacterAdded:Connect(function(char)
		while not MorphService do task.wait() end --Waits for MorphService to finish		
		for _, morph in pairs(Morphs) do
			if plr.Team == morph.Team and plr:GetRankInGroup(morph.GroupId) == morph.RankId then	
				MorphService:EquipMorph(char, morph.Morph)
			end
		end
	end)
end)

I’ll try it and see if it works.

I tried it and it doesn’t appear to work. I added a print after it waits for the module and it doesn’t print anything.

How would I go about doing that?

local function plrAdded(plr)
    plr.CharacterAdded:Connect(function()
        while not MorphService do task.wait() end --Waits for MorphService to finish		
    	for _, morph in pairs(Morphs) do
		    if plr.Team == morph.Team and plr:GetRankInGroup(morph.GroupId) == morph.RankId then	
		    	MorphService:EquipMorph(char, morph.Morph)
	    	end
	    end
    end)
 end

Players.PlayerAdded:Connect(plrAdded)

for _, plr in pairs(Players:GetPlayers()) do
    plrAdded(plr)
end

Ok, I understand it but I don’t see why the CharacterAdded is there where plr would be nil…

Didn’t realize the mistake I made. I’ve edited the post.

I got help from a friend and somehow this works without issue,

game.Players.PlayerAdded:Connect(function(plr)

end)