Lag while loading players

Greetings, everyone.
In one of my games, I have noticed a big lag coming from our systems and I have no idea why, but I most likely think it is related to the lag the game is handing (65+ players & big detailed map)

But either way, it should not take too long to load players. The video of it loading: https://youtu.be/ZkNP9pP7XJ4
Details:
When you press the refresh icon, it runs the ReloadList() function that loads the code.
The function is the following: (assuming Reloading variable is a boolean)

function ReloadList(studentCallback)
	if Reloading then return end
	print('[C][SBLS] Reloading..')
	Reloading = true
	local StudentListFrame = QueryTab:WaitForChild('PlayerList')
	local List = StudentListFrame:WaitForChild('List')
	
	ClearSL()
	
	QueryTab:WaitForChild("PlayerList"):WaitForChild("Reload").Visible = false
	QueryTab:WaitForChild("PlayerList"):WaitForChild("SearchUser").Visible = false
	QueryTab:WaitForChild("PlayerList"):WaitForChild("TextLabel").Visible = true

	for _, Player in ipairs(PlayerList) do
		if Player:GetRankInGroup(GroupID) <= StudentRankId then
			local template = List:WaitForChild("TEMPLATE_USER")
			local UserBlock = template:Clone();

			UserBlock.Name = Player.Name:lower()
			UserBlock.CanDelete.Value = true;
			UserBlock.UserImage.Image = Players:GetUserThumbnailAsync(Player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size60x60);
			UserBlock.Username.Text = Player.Name;
			UserBlock.Year.Text = Player:GetRoleInGroup(GroupID);
			UserBlock.Visible = true;
			UserBlock.Parent =  List;

			UserBlock.LoadData.LoadDataButton.MouseButton1Click:Connect(function() 
				if not Players:FindFirstChild(Player.Name) then return end;
				PlayerListprogress = false;
				StudentListFrame.Visible = false;
				--StudentListFrame:Destroy()
				LoadStudentBehaviourLogs(Player, function()
					studentCallback()
				end)
			end)				

			UserBlock.Oncall.Button.MouseButton1Click:Connect(function() 
				if not Players:FindFirstChild(Player.Name) then return end;
				PlayerListprogress = false;
				StudentListFrame.Visible = false;
				--StudentListFrame:Destroy()
				CreateOncallSession(Player, function()
					studentCallback()
				end)
			end)

			UserBlock.Profile.Button.MouseButton1Click:Connect(function() 
				if not Players:FindFirstChild(Player.Name) then return end;
				PlayerListprogress = false;
				StudentListFrame.Visible = false;
				--StudentListFrame:Destroy()
				LoadStudentProfile(Player, function()
					studentCallback()
				end)
			end)
		end
	end
	
	QueryTab:WaitForChild("PlayerList"):WaitForChild("Reload").Visible = true
	QueryTab:WaitForChild("PlayerList"):WaitForChild("SearchUser").Visible = true
	QueryTab:WaitForChild("PlayerList"):WaitForChild("TextLabel").Visible = false
	
	print("[C][SBLS] Finished reloading")
	Reloading = false
end	

And the PlayerList variable is a table consisting of every player in the server, broadcasted by the server every 1 second on a different thread using task.spawn.

Any help is much appreciated, thank you

well i think thats a pretty basic amount of time, considering there are 65 players

It sometimes decides to not load entirely, which is the main reason I am making this post and I’m trying to figure out why

This sometimes doesn’t load entirely because Player:GetRankInGroup is an async function. It stops the current thread until the next one runs. What you should do is wrap all of this in a task.spawn function:

	for _, Player in ipairs(PlayerList) do 
    task.spawn(function()
		if Player:GetRankInGroup(GroupID) <= StudentRankId then
			local template = List:WaitForChild("TEMPLATE_USER")
			local UserBlock = template:Clone();

			UserBlock.Name = Player.Name:lower()
			UserBlock.CanDelete.Value = true;
			UserBlock.UserImage.Image = Players:GetUserThumbnailAsync(Player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size60x60);
			UserBlock.Username.Text = Player.Name;
			UserBlock.Year.Text = Player:GetRoleInGroup(GroupID);
			UserBlock.Visible = true;
			UserBlock.Parent =  List;

			UserBlock.LoadData.LoadDataButton.MouseButton1Click:Connect(function() 
				if not Players:FindFirstChild(Player.Name) then return end;
				PlayerListprogress = false;
				StudentListFrame.Visible = false;
				--StudentListFrame:Destroy()
				LoadStudentBehaviourLogs(Player, function()
					studentCallback()
				end)
			end)				

			UserBlock.Oncall.Button.MouseButton1Click:Connect(function() 
				if not Players:FindFirstChild(Player.Name) then return end;
				PlayerListprogress = false;
				StudentListFrame.Visible = false;
				--StudentListFrame:Destroy()
				CreateOncallSession(Player, function()
					studentCallback()
				end)
			end)

			UserBlock.Profile.Button.MouseButton1Click:Connect(function() 
				if not Players:FindFirstChild(Player.Name) then return end;
				PlayerListprogress = false;
				StudentListFrame.Visible = false;
				--StudentListFrame:Destroy()
				LoadStudentProfile(Player, function()
					studentCallback()
				end)
			end)
            end)
		end

(Also might be good to wrap this in pcall too, I’m not sure if Player:GetRankInGroup() can error.)

1 Like

Thank you. This has reduced the lag level.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.