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?
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.
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)
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.
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.