I’ve been trying to figure out an alternative to this, however, so far I’ve come empty handed. I did try to make the code run each time a player joins, but, the custom leaderboard I’m using broke, as shown below
Here’s the code that works, but is wayy too fast and is causing lag.
for _, team in pairs(game.Teams:GetChildren()) do
if ServiceManager.PlayerListVisible == true then
if Settings.teams.DisplayingAllTeams then
PlayerList[team.Name].Visible = true
else
if #team:GetPlayers() == 0 then
PlayerList[team.Name].Visible = false
else
PlayerList[team.Name].Visible = true
end
end
else
return
end
end
end
You should probably use PlayerList:FindFirstChild(team.Name) instead of trying to index it and also making sure that it could find the child. But I’m assuming the issue is happening because the teams on the player list aren’t generating fast enough. You need a way to wait until all the teams are inside the player list before looping
Don’t link this to an iteration, instead link it to an event. Using an infinite loop will cause a lot of memory strain. A good way would be listening for a player join and then their team change, remember to disconnect connections when they leave though. If you made your own version of RBXScriptSignal, you could use your own custom signals.
I’m now facing a new errror of attempt to index nil with visible, and I presume its because the script is unable to find the team…
for _, team in pairs(game.Teams:GetChildren()) do
if ServiceManager.PlayerListVisible == true then
if Settings.teams.DisplayingAllTeams then
PlayerList.FindFirstChild(team.Name).Visible = true
else
if #team:GetPlayers() == 0 then
PlayerList:FindFirstChild(team.Name).Visible = false
else
PlayerList:FindFirstChild(team.Name).Visible = true
Find first child returns nil if it can’t find the child, so you need to do an if statement to check if the child exists, and if it doesn’t you can just continue the loop. But that’s only going to solve the error, not the issue
Another option is to manually code a fixed rate of your choosing:
local rate = 0.5 -- Ticks occur every x real seconds
local elapsed = 0
RunService.Heartbeat:Connect(function(dt)
elapsed += dt
while elapsed > rate do -- Do as many ticks needed to match real time
elapsed -= rate
-- TODO your code here
end
end)