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!
-
What do you want to achieve? I want to fix the errors in my leaderboard script and finally get it working.
-
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.
-
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!