PlayerAdded:Connect isn't firing!

This is a simple Server Script used to determine the players Information above there head.

local Players = game:GetService("Players")
local groupSave

local allies = {}
local groupID = 6146409

local function GetCardWithInfo(player, CardLevel, group)
	for Num,card in pairs(game.ServerStorage.Cards:GetChildren()) do
		for _, level in pairs(card:GetChildren()) do
			if level:IsA("IntValue") then
				if level.Value == CardLevel then
					local CloneCard = card:Clone()
					CloneCard.Parent = player.Backpack
					break
				end
			end
		end
	end
end

local pages = game:GetService("GroupService"):GetAlliesAsync(groupID)
while true do
	for _,group in pairs(pages:GetCurrentPage()) do
		table.insert(allies, group)
	end
	if pages.IsFinished then
		break
	end
	pages:AdvanceToNextPageAsync()

end

Players.PlayerAdded:Connect(function(player)
	for _,group in pairs(allies) do
		if player:IsInGroup(group.Id) then
			groupSave = group
			local Rank = player:GetRankInGroup(group.Id)
			if Rank <= 2 then
				GetCardWithInfo(player, 0, group)
			elseif Rank <= 5 then
				GetCardWithInfo(player, 1, group)
			elseif Rank <= 10 then
				GetCardWithInfo(player, 2, group)
			elseif Rank <= 15 then
				GetCardWithInfo(player, 3, group)	
			elseif Rank >= 20 then
				GetCardWithInfo(player, 4, group)	
			elseif Rank <= 25 then
				GetCardWithInfo(player, 5, group)
			elseif Rank <= 30 then
				GetCardWithInfo(player, 6, group)
			elseif Rank >= 40 then
				GetCardWithInfo(player, 7, group)
			end
			print("Requesting Card and Info.")
		else
			print("NOPE")
		end
	end
	
	player.CharacterAdded:Connect(function(char)
		local CloneInfo = game.ServerStorage.InformationUI:Clone()
		CloneInfo.Parent = player.PlayerGui
		CloneInfo.MainFrame.PlayerName.Text = player.Name
		if groupSave then
			CloneInfo.MainFrame.Department.Text = groupSave.Name
			CloneInfo.MainFrame.Rank.Text = player:GetRoleInGroup(groupSave.Id)
		end
		CloneInfo.Adornee = char:WaitForChild("Head")
		print("LOADED")
	end)
end)

For some reason, The “PlayerAdded:Connect()” is not firing. Is there any thing I can do to fix it?

the fourth to last “end” has to be “end)”, hope this helps

can you specify the line number?

You have a while true before you connect the function…

PlayerAdded will still fire, the problem is that you have a yielding function being called before the function is connected. This causes the connection to be deferred (or delayed?), so any player who joins before the connection line runs will not have the function called on them.

The way to resolve this is to create a named function that you can connect to PlayerAdded as well as run for any players existing in the server before the connection is added. This is a good pattern regardless of if you have a yielding function being called before or not, honestly.

-- All your code before PlayerAdded comes here

local function playerAdded(player)
    -- Move your PlayerAdded code here: skip the Connect line as well
    -- as the final end with the closing parenthesis.
end

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end

@steven4547466 The loop has a break condition if IsFinished is true after the current page is iterated.

3 Likes

sorry indentation is hard to read cuz I’m on mobile, deleted my replys, thanks

also the end I was initially talking about is for that for loop not the while loop, which I didn’t realize at all at first

Thanks a lot, this helped! it works now.

I mean it worked after I moved

local pages = game:GetService("GroupService"):GetAlliesAsync(groupID)

while true do
	print("run")
	for _,group in pairs(pages:GetCurrentPage()) do
		table.insert(allies, group)
	end
	if pages.IsFinished then
		print("break")
		break
	end
	pages:AdvanceToNextPageAsync()

end

to the very end of the script (I added the prints)
tg56z_90791

1 Like

This happens because there’s no longer a yielding scope deferring the PlayerAdded connection. One of the problems with this pattern is that if the allies table needs to have members to check during a PlayerAdded connection running it may not be able to access them. I don’t condone the original method either but eventually you would need to yield if the members you require are not accessible.

1 Like

It works but I think it won’t work with group collections since when the player joins first it wont have a group to for loop in with. same thing I never thought the yeilding would be this big of a deal… but it is.

Yeah, I missed the point of your initial loop, my bad