GUI is only showing up sometimes

I’m making a global leaderboard GUI using cloning of a template I created and only sometimes they are showing up, although they are supposed to be visible.
(When it does show up, it’s only showing the first place)

The script (in ServerScriptService):

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("Data")

local function updateLeaderboard()
    local success, err = pcall(function()
        local Data = DataStore:GetSortedAsync(false, 5)
        local KillsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(KillsPage) do
			local username = "Failed to load name"
			local success, err = pcall(function()
				username = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			end)
			print(username)
            local Name = username
            local Kills = data.value
            local isOnLeaderboard = false
            for i, v in pairs(game.StarterGui.MainMenuGui.Leaderstats.Holder.ScrollingFrame:GetChildren()) do
                if v.Username.Text == Name then
                    isOnLeaderboard = true
                    break
                end
            end
			
            if Kills and isOnLeaderboard == false then
				local newFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
                newFrame.Username.Text = Name
				newFrame.Kills.Text = Kills
				newFrame.Rank.Text = Rank
				newFrame.Position = UDim2.new(0, 0, newFrame.Position.Y.Scale + (0.048 * #game.StarterGui.MainMenuGui.Leaderstats.Holder.ScrollingFrame:GetChildren()), 0)
				newFrame.Parent = game.StarterGui.MainMenuGui.Leaderstats.Holder.ScrollingFrame
            end
        end
    end)

    if not success then
        warn(err)
    end
end

while true do
    for _, player in pairs(game.Players:GetPlayers()) do
        DataStore:SetAsync(player.UserId, player.stats.Kills.Value)
    end

	for _, frame in pairs(game.StarterGui.MainMenuGui.Leaderstats.Holder.ScrollingFrame:GetChildren()) do
        frame:Destroy()
    end

    updateLeaderboard()
    print("Leaderboard Updated")

    wait(math.random(40, 60))
end

Test 1:

Test 2:

This might be the problem:

for i, v in pairs(game.StarterGui.MainMenuGui.Leaderstats.Holder.ScrollingFrame:GetChildren()) do
--and
for _, frame in pairs(game.StarterGui.MainMenuGui.Leaderstats.Holder.ScrollingFrame:GetChildren()) do

You are looping through the StarterGui folder ui instead of the ui that is in the players PlayerGui folder.

So you could try looping through all the players first then loop through each of those players PlayerGui folders.

A frame for each player is cloning (almost) properly with the username, rank, and data; the only problem is that sometimes the GUI becomes invisible even if Visible is true.

Are the contents inside the frame still positioned where they are supposed to be positioned?
Please check that.

1 Like

They are where they are supposed to be

You’re referencing the StarterGui which is only given to players when they first spawn in, try putting the script inside the MainMenuGui and changing it to

script.Parent.Leaderstats.Holder.ScrollingFrame
1 Like

Thanks! Everything is working properly now