Hey Everyone,
I’m making a datastore script, but it’s not working. It does not save the data.
Here is my script:
local Players = game:GetService('Players')
local DataStoreService = game:GetService('DataStoreService')
local WinsDataStore = DataStoreService:GetDataStore('Wins')
Players.PlayerAdded:Connect(function(Player)
local Stats = Instance.new('Folder')
Stats.Name = 'leaderstats'
Stats.Parent = Player
local Wins = Instance.new('IntValue')
Wins.Name = 'Wins'
Wins.Parent = Stats
local Data = WinsDataStore:GetAsync(Player.UserId)
if Data then
for name, value in pairs(Data.Stats) do
Stats[name].Value = value
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
for _, Player in pairs(game.Players:GetPlayers()) do
local SaveData = {Stats = {}}
for _,stat in pairs(Player.leaderstats:GetChildren()) do
SaveData[stat.Name] = stat.Value
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
end
wait(2)
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
local SaveData = {Stats = {}}
for _,stat in pairs(Player.leaderstats:GetChildren()) do
SaveData[stat.Name] = stat.Value
end
WinsDataStore:SetAsync(Player.UserId,SaveData)
end
wait(2)
end)
local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')
local GlobalDataStore = DataStoreService:GetOrderedDataStore('WinsGlobalNew')
local winsBoard = workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.GlobalBoard
local winsTemplate = winsBoard.SurfaceGui.Leaderboard.Template:Clone()
winsBoard.SurfaceGui.Leaderboard.Template:Destroy()
local function update()
for _,child in pairs(winsBoard.SurfaceGui.Leaderboard:GetChildren()) do
if child:IsA('Frame') then
child:Destroy()
end
end
local success,err = pcall(function()
local data = GlobalDataStore:GetSortedAsync(false,50)
local page = data:GetCurrentPage()
for rank,plrData in ipairs(page) do
local userid = plrData.key
local wins = plrData.value
if rank <= 3 then
local npc = workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs:FindFirstChild(rank)
if npc then
npc.UserId.Value = userid
end
end
local new = winsTemplate:Clone()
new.PlrName.Text = Players:GetNameFromUserIdAsync(userid)
new.PlrAmount.Text = wins
new.LayoutOrder = rank
new.Parent = winsBoard.SurfaceGui.Leaderboard
end
end)
end
while true do
update()
wait(math.random(120,180))
spawn(function()
for _, Player in pairs(game.Players:GetPlayers()) do
GlobalDataStore:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
wait(math.random(2,4))
end
end)
end
Do you know why my leaderboard is empty if I have 3 wins?
local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')
local GlobalDataStore = DataStoreService:GetOrderedDataStore('WinsGlobalNew')
local winsBoard = workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.GlobalBoard
local winsTemplate = winsBoard.SurfaceGui.Leaderboard.Template:Clone()
winsBoard.SurfaceGui.Leaderboard.Template:Destroy()
local function update()
for _,child in pairs(winsBoard.SurfaceGui.Leaderboard:GetChildren()) do
if child:IsA('Frame') then
child:Destroy()
end
end
local success,err = pcall(function()
local data = GlobalDataStore:GetSortedAsync(false,50)
local page = data:GetCurrentPage()
for rank,plrData in ipairs(page) do
local userid = plrData.key
local wins = plrData.value
if rank <= 3 then
local npc = workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs:FindFirstChild(rank)
if npc then
npc.UserId.Value = userid
end
end
local new = winsTemplate:Clone()
new.PlrName.Text = Players:GetNameFromUserIdAsync(userid)
new.PlrAmount.Text = wins
new.LayoutOrder = rank
new.Parent = winsBoard.SurfaceGui.Leaderboard
end
end)
end
while true do
update()
wait(math.random(120,180))
spawn(function()
for _, Player in pairs(game.Players:GetPlayers()) do
GlobalDataStore:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
wait(math.random(2,4))
end
end)
end
local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')
local GlobalDataStore = DataStoreService:GetOrderedDataStore('WinsGlobalNew')
local winsBoard = workspace.Leaderboards.WinsLeaderboard.WinsLeaderboard.GlobalBoard
local winsTemplate = winsBoard.SurfaceGui.Leaderboard.Template:Clone()
winsBoard.SurfaceGui.Leaderboard.Template:Destroy()
local function update()
for _,child in pairs(winsBoard.SurfaceGui.Leaderboard:GetChildren()) do
if child:IsA('Frame') then
child:Destroy()
end
end
local success,err = pcall(function()
local data = GlobalDataStore:GetSortedAsync(false,50)
local page = data:GetCurrentPage()
for rank,plrData in ipairs(page) do
local userid = plrData.key
local wins = plrData.value
if rank <= 3 then
local npc = workspace.Leaderboards.WinsLeaderboard.WinsPodium.NPCs:FindFirstChild(rank)
if npc then
npc.UserId.Value = userid
end
end
local new = winsTemplate:Clone()
new.PlrName.Text = Players:GetNameFromUserIdAsync(userid)
new.PlrAmount.Text = wins
new.LayoutOrder = rank
new.Parent = winsBoard.SurfaceGui.Leaderboard
end
end)
if not success then
warn('An error occured, '..err)
else
print('Success')
end
end
while true do
update()
wait(math.random(120,180))
spawn(function()
for _, Player in pairs(game.Players:GetPlayers()) do
GlobalDataStore:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
wait(math.random(2,4))
end
end)
end