Why doesnt this work? Stats script and global leaderboard script.
This question has been solved by the original poster.
local DataStore = game:GetService("DataStoreService") -- stats script
local ds1 = DataStore:GetOrderedDataStore("Survivals")
local ds2 = DataStore:GetOrderedDataStore("TopSurvivals")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local survivals = Instance.new("IntValue", leaderstats)
survivals.Name = "Survivals"
local survivalsVal = ds2:GetAsync(player.UserId)
survivals.Value = ds1:GetAsync(player.UserId) or 0
ds1:SetAsync(player.UserId, survivals.Value)
ds2 = survivalsVal
survivals.Changed:Connect(function()
print('Saving data')
ds1:SetAsync(player.UserId, survivals.Value)
print(player.UserId.."'s Data of"..survivals.Value.." "..survivals.Name.." has been saved")
ds2:SetAsync(player.UserId, survivals.Value)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local survivals = player.leaderstats.Survivals
print('Saving data')
ds1:SetAsync(player.UserId, player.leaderstats.Survivals.Value)
print(player.UserId.."'s Data of"..player.leaderstats.Survivals.Value.." "..player.leaderstats.Survivals.Value.." has been saved")
ds2:SetAsync(player.UserId, survivals.Value)
end)
-- Global leaderboard script
local ds1 = game:GetService('DataStoreService'):GetOrderedDataStore("TopSurvivals")
while true do
local Ascending = false
local PageSize = 4
local pages = ds1:GetSortedAsync(Ascending, PageSize)
local CurrentPage = pages:GetCurrentPage()
for i,v in pairs(script.Parent:GetChildren()) do
if v.Name ~= "Label" and v ~= script then
v.Visible = false
end
end
for i,v in ipairs(CurrentPage) do
local key = v.key
local value = v.value
local text = script.Parent:FindFirstChild("Position" .. i)
text.Visible = true
text.Username.Text = game.Players:GetNameFromUserIdAsync(key)
text.Survivals.Text = value
end
wait(60)
end