For i,v in pairs() not running

I am creating a leaderboard, although this function for i,v in pairs() is not running.

This is required to stop multiple frames to clone, all displaying the same username. I was looking around a lot on the DevForums, but nothing suited the response I was hoping for. Thank you a lot for even reaching out to this post, and I will put the script down below.



local DataStoreService = game:GetService("DataStoreService")
local RobuxLeaderboard = DataStoreService:GetOrderedDataStore("RobuxLeaderboard")

local function UpdateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = RobuxLeaderboard:GetSortedAsync(false,5)
		local RobuxPage = Data:GetCurrentPage()
		for Rank, data in ipairs(RobuxPage) do
			local username = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = username
			local Robux = data.value
			local isOnLeaderboard = false 
			
			-- SECTION, Not Working
			
			print("works") -- landmine #1
			for i,v in pairs(game.StarterGui.ScreenGui.LBFrame.Holder:GetDescendants()) do
				print(v.NameLabel.Text..Name)
				if v.NameLabel.Text == Name then
					isOnLeaderboard = true
					print("does work :palm: ")
					break
				end
			end
			
			-- SECTION, Not Working
			
			print("works 2") -- pooper #1
			
			if Robux and isOnLeaderboard == false then
				print("works 3 :bruh:") -- i wanna fart rn
				local NewLBFrame = script.Parent.Sample:Clone()
				NewLBFrame.NameLabel.Text = Name
				NewLBFrame.Visible = true
				NewLBFrame.CashLabel.Text = Robux
				NewLBFrame.RankLabel.Text = "#"..Rank
				NewLBFrame.Position = UDim2.new(0,0,NewLBFrame.Position.Y.Scale + (.08 * #game.StarterGui.ScreenGui.LBFrame.Holder:GetChildren()))
				NewLBFrame.Parent = script.Parent
			end
		end
	end)
	
	if not success then
		print(errorMessage)
	end
end

while true do
	for _, Player in pairs(game.Players:GetPlayers()) do
		RobuxLeaderboard:SetAsync(Player.UserId, Player.Stats.Robux.Value)
	end
	
	for _, frame in pairs(game.StarterGui.ScreenGui.LBFrame.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	UpdateLeaderboard()
	print("Updated!")
	
	wait(10)
	
end


Thanks again,

Sincerely, PuzzledDevs

2 Likes

May i ask which prints prints- print()'s prints

Prints text label name, and the player’s name together (Doesn’t error nor run)

nope i mean which prints does print?

like which lines?

OH, it prints all but i,v in pairs().

the prints with things like #landmines-?

also may i see what your leaderboards looks like in editor?

Yes, its just funny labels in it lol

also tryna print this line and tell me whats the result:

print(Name, Robux)

just trying troubleshoots, since i cant really solve this easily, the code looks alright and i dont see any line thatll produce errors.

The only reason everything but the prints within the loop would run would be because there is nothing to loop through. Print game.StarterGui.ScreenGui.LBFrame.Holder:GetDescendants() right before the loop and I’m willing to bet it will be an empty list

In fact, you’re clearing the Holder frame immediately before calling UpdateLeaderboard().

also yep this too, thats why i wanna see how it looks like in explorer.

wait in the updateLeaderboard(), it creates a new one-? OHHHH

oi tryna put the second loop before the first loop. in the updateLeaderboard()

Wouldn’t the function be remained as undefined then?

that before this


for i,v in pairs(game.StarterGui.ScreenGui.LBFrame.Holder:GetDescendants()) do
				print(v.NameLabel.Text..Name)
				if v.NameLabel.Text == Name then
					isOnLeaderboard = true
					print("does work :palm: ")
					break
				end
			end

no the one inside the updateLeaderboard()

I did and it still doesn’t work

thats weird.

so how i see your code is

leaderboard clears first (deletes everything) then calls the updateLeaderboard()

which if you change the position of the line that creates the leaderboardFrame, before the for i loop that checks if “isOnLeaderboard”

then that should work… thats weird

Yeah, I’m not gonna lie

Theres been a NEW error message after I did what you asked lol. Down here

NameLabel is not a valid member of UIListLayout “StarterGui.ScreenGui.LBFrame.Holder.UIListLayout”, which I now get but it’s a bit confusing to clear up

wait wait wait. let me try something in studio brb.