Global Leaderboard - Not working properly


This is with name line being commented out.

This is with it not commented out:

Code that is having error:

local success, err = pcall(function()
			local data = KillDataStore:GetSortedAsync(false, 50)
			local page = data:GetCurrentPage()
			
			for ranking, dataStored in ipairs(page) do
				local userID = string.split(dataStored.key, "_")[2]
				local name = game.Players:GetNameFromUserIdAsync(userID)
				local kills = dataStored.value
				print(name) -- returns correct name
				local template = script.Template:Clone()
				template["Name"].Text = name -- with this its broke, without it it works, like wtf??
				template["Rank"].Text = "#" .. tostring(ranking)
				template["Kills"].Text = tostring(kills)
				template.Parent = script.Parent.Content
			end
		end)

I’ve tried everything I could think of, none of it works.

Full code
local DataStore = game:GetService("DataStoreService")
local KillDataStore = DataStore:GetOrderedDataStore("KillData")

local ResetTime = script.ResetTime.Value
while wait(1) do
	ResetTime = ResetTime - 1
	script.Parent.Timer.Text = "The leaderboard resets in <b>" .. ResetTime .. "</b> seconds."
	
	if ResetTime == 0 then
		ResetTime = script.ResetTime.Value
		for _, player in pairs(game.Players:GetPlayers()) do
			local success, err = pcall(function()
				KillDataStore:SetAsync("Player_"..player.UserId, player:WaitForChild("leaderstats").Kills.Value)
			end)

			if success then
				print("Success!")
			end
		end
		
		for _, leaderboardFrame in pairs(script.Parent.Content:GetChildren()) do
			if leaderboardFrame.ClassName == "Frame" then
				leaderboardFrame:Destroy()
			end
		end
		
		local success, err = pcall(function()
			local data = KillDataStore:GetSortedAsync(false, 50)
			local page = data:GetCurrentPage()
			
			for ranking, dataStored in ipairs(page) do
				local userID = string.split(dataStored.key, "_")[2]
				local name = game.Players:GetNameFromUserIdAsync(userID)
				local kills = dataStored.value
				print(name) -- returns correct name
				local template = script.Template:Clone()
				template["Name"].Text = name -- with this its broke, without it it works, like wtf??
				template["Rank"].Text = "#" .. tostring(ranking)
				template["Kills"].Text = tostring(kills)
				template.Parent = script.Parent.Content
			end
		end)
	end
end

The problem lies here. Player is an instance, not a string. Instead, do this:

local name = game.Players:GetNameFromUserIdAsync(userID).Name

EDIT: I’m silly! You should do a print(name) to figure out what the problem is first. Talk to me when you have the print, and possibly the error given in the output.

Well he is using game.Players:GetNameFromUserIdAsync(userID) , so basically he is getting the Name not the Player Instance , so it should work fine . I assume whether its because the UserId might be nil or something.

1 Like

When I print the name, it comes through as PMGDesigns.

Then it should work Fine , Can you Show whats the error ?

There is no error, however when the line is commented out, the template is parented to my frame.
When it’s not commented out, it just doesn’t.

Error continues to persist. Any idea what it could be?

I don’t See anything Wrong , Can you Make Sure that template has a TextLabel Named ‘Name’ . Also are you sure that there is no Error in the Output?

The issue lies in the indexing of the template with [“Name”], essentially instead of going to the child named “Name”, its pulling up the Name property of template. So, when you attempt to set the text of the name property of your template, it’ll throw an error. The best way to handle your error is renaming your “Name” instance to “Username”. If this yields no fruit please let me know.

3 Likes

I didn’t see this post until I fixed it with a shower thought ahah, thank you though.
This would have been the solution. :slight_smile: