In my game, I have this global leaderboard system. The Part setting the value works fine including saving and getting the data, however, always throws a nil when it comes to update() function. I tried to debug but I still can`t figure out. Any Helps will be great. Thankyou.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local GlobalWins = DataStoreService:GetOrderedDataStore("playerWins")
local Pages = GlobalWins:GetSortedAsync(false, 10)
local Data = Pages:GetCurrentPage()
local leaderScreen1 = workspace.globalLeaderBoard.screen.SurfaceGui1
local template = leaderScreen1.template
local leaderScreen2 = workspace.globalLeaderBoard.screen.SurfaceGui2
local template2 = leaderScreen1.template
local function giveFolders(player)
print("starting winGive")
if player then
local dataFolder = player:WaitForChild(player.Name.."dataFolder")
local winValue = Instance.new("NumberValue",dataFolder)
winValue.Name = "Wins"
winValue.Parent = dataFolder
local winDataRetrive
local success, result = pcall(function()
winDataRetrive = GlobalWins:GetAsync(player.UserId)
end)
if success then
winValue.Value = tonumber(winDataRetrive) --returns correct value
print(tonumber(winDataRetrive)) -- nvm. This also returned the correct Value.
else
warn(result)
end
end
end
local function saveData(player)
if player then
local dataFolder = player:FindFirstChild(player.Name.."dataFolder")
local winValue = dataFolder:FindFirstChild("Wins")
if winValue then
local success, result = pcall(function()
return GlobalWins:UpdateAsync(player.UserId,function()
return winValue.Value
end)
end)
if success then
print("Saved Value")
print(winValue.Value)
else
warn(result)
end
end
end
end
game.Players.PlayerAdded:Connect(function(plr)
print("Detected")
if plr then
giveFolders(plr)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
if plr then
saveData(plr)
end
end)
local function update()
for i, v in pairs(leaderScreen1.MainFrame:GetChildren()) do
if v:IsA("Frame") then
v:Destroy()
end
for i, v in pairs(leaderScreen2.MainFrame:GetChildren()) do
if v:IsA("Frame") then
v:Destroy()
end
end
for i,v in pairs(Data) do
local username = Players:GetNameFromUserIdAsync(v.key)
local value = v.Value
local newTemplate = template:Clone()
newTemplate.Name = username
newTemplate.frameName.Text = tostring(username)
newTemplate.frameValue.Text = tostring(value)
newTemplate.Visible = true
newTemplate.Parent = leaderScreen1.MainFrame
end
for i,v in pairs(Data) do
local username = Players:GetNameFromUserIdAsync(v.key)
local value = tonumber(v.Value)
print(tostring(value))
local newTemplate = template2:Clone()
newTemplate.Name = username
newTemplate.frameName.Text = tostring(username)
newTemplate.frameValue.Text = tostring(value)
newTemplate.Visible = true
newTemplate.Parent = leaderScreen2.MainFrame
end
wait(60*3)
end
end
update()
spawn(function()
while true do
update()
end
end)
By the way, the actual script where it updates the point is in different place(in same game) and that also works totally fine.

1 comment
Alternatively, I used the method of getting value from GetAsync(v.key), which somehow works. However, I am still looking for the solution to this script. Thanks for all your help