Errors in my Global Leaderboard script

Forgive me if my code isn’t all that great. I am still trying to learn how to do it. This is also my first time using the DevForum! Ask any me any questions and I will try to get back to you!

  1. What do you want to achieve? I want to fix the errors in my leaderboard script and finally get it working.

  2. What is the issue? Sometimes, when I run the script nothing appears on the leaderboard but the output also doesn’t show any errors with the script. Other times when I run the script it will just give me an error saying “Argument 1 missing or nil” that I have narrowed down to line 13 using some prints.

  3. What solutions have you tried so far? I kind of skimmed the data stores article as I don’t understand ordered data stores and normal data stores all that well. I have double checked the YT video multiple times and I seem to have everything word for word except for my own variables that I want to use.

After that, you should include more details if you have any. I followed this tutorial to help me make the script. The script is in a server script in ServerScriptService.

Here is a test place if you want a copy of the set up.

Leaderboard Script
local DataStore = game:GetService("DataStoreService")
local GaiaStore = DataStore:GetOrderedDataStore("Gaia")



local function updateLeaderbaord()
	local success, err = pcall(function()
		local Data = GaiaStore:GetSortedAsync(false, 5)
		local GaiaPage = Data:GetCurrentPage()
		print("1")
		for Rank, data in ipairs(GaiaPage) do
			print("2")
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key)) -- Line 13 for reference
			print("2-3")
			local name = userName
			local Gaia = data.Value
			print("3")
			local isOnLeaderboard = false
			for i,v in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGui.Holder:GetChildren()) do
				if v.Player.Text == name then
					isOnLeaderboard = true
					break
				end
			end
			print("4")

			if Gaia and isOnLeaderboard == false then
				local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				newLbFrame.Player.Text = name
				newLbFrame.Gaia.Text = Gaia
				newLbFrame.Rank.Text = "#"..Rank
				newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * game.Workspace.GlobalLeaderboard.LeaderboardGui.Holder:GetChildren()),0)
				newLbFrame.Parent = game.Workspace.GlobalLeaderboard.LeaderboardGui.Holder
				print("5")
			else
				print("6")
			end
		end

	end)

	if not success then
		warn(err)
	end
end

while true do
	
	for _, player in pairs(game.Players:GetPlayers()) do
		GaiaStore:SetAsync(player.UserId, player.ValFold.GaiaExp)
	end
	
	for _, frame in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGui.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderbaord()
	print("Leaderboard Updated")
	
	wait(60)
	
end
Player Joined/Leaving Script (If relevant)
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local GaiaStore = DataStore:GetDataStore("Gaia")


Players.PlayerAdded:Connect(function(plr)
	local GaiaKey = "Gaia-"..plr.UserId
	local defaultGaia = 0
	
	local valFold = Instance.new("Folder")
	valFold.Name = "ValFold"
	valFold.Parent = plr
	
	local RegionVal = Instance.new("StringValue")
	RegionVal.Name = "RegionVal"
	RegionVal.Value = "Neutral"
	RegionVal.Parent = valFold
	
	local GaiaExp = Instance.new("IntValue")
	GaiaExp.Name = "GaiaExp"
	GaiaExp.Parent = valFold
	
	local myGaia
	local success, err = pcall(function() -- Access the data store for Gaia Essence amount
		myGaia = GaiaStore:GetAsync(GaiaKey)
	end)
	
	if success then
		GaiaExp.Value = myGaia
		print("Previous GaiaExp data loaded")
	else
		if err == nil then
			GaiaExp.Value = defaultGaia
			print("Used default gaia")
		else
			print("Data LOAD error for "..plr.Name.." - Player ID "..plr.UserId)
			warn(err)
		end
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local GaiaKey = "Gaia-"..plr.UserId
	local GaiaExp = plr.ValFold.GaiaExp
	
	local success, err = pcall(function() -- Update the data store on the Gaia Essence amount
		GaiaStore:SetAsync(GaiaKey, GaiaExp.Value)
	end)
	
	if success then
		print("Successfully saved "..plr.Name.." data")
	else
		print("Data SAVE error for "..plr.Name.." - Player ID "..plr.UserId)
		warn(err)
	end

end)

game:BindToClose(function()
	local RunService = game:GetService("RunService")

	if RunService:IsStudio() then
		return
	else
		wait(3)
	end
	
end)


I am open to any suggestions or criticism you might have so that I can improve myself. Thanks everyone!

1 Like

Alright let me start of by saying. The problem you’re facing is not an error in the coding, it’s simply a logic error and you just need to reread over your code.

You have 3 errors in the script from what i’ve read through so far.

while true do
	for _, player in pairs(game.Players:GetPlayers()) do
		GaiaStore:SetAsync(player.UserId, player.ValFold.GaiaExp)
	end

	for _, frame in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGui.Holder:GetChildren()) do
		frame:Destroy()
	end

	updateLeaderbaord()
	print("Leaderboard Updated")

	wait(60)
end

In the first loop, you tried setting the value of an instance to datastore. Simply make sure to call the Value property on the NumberValue/IntValue.

local Gaia = data.Value

You uppercased Value when it’s simply just data.value.

if Gaia and isOnLeaderboard == false then
	local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
	newLbFrame.Player.Text = name
	newLbFrame.Gaia.Text = Gaia
	newLbFrame.Rank.Text = "#"..Rank
    newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * game.Workspace.GlobalLeaderboard.LeaderboardGui.Holder:GetChildren()),0)
    newLbFrame.Parent = game.Workspace.GlobalLeaderboard.LeaderboardGui.Holder
	print("5")
else

For the positioning line you grab a table, which you can’t just multiply, i’m not fully sure what you’re trying to do here but make sure to put a hashtag in front of a table if you want to get how much values/instances are in the table.

I hope this helps you a lot. Maybe in the future instead of printing numbers over the script to see where it is erroring. Try also printing some values that are in the script to see if any of them are nil or not correct information.

1 Like

Thank you so much! You’re a lifesaver! Also, thanks for the tip about printing values I will keep that in mind for the future :D!