Hello,
After this topic, I create a new one because I think I know where my problem comes from, but I can’t solve it.
This is why I’m creating a new topic to maybe get more answers. (Here is the old topic DataStoreService - OrderedDataStore for a leaderboard - #7 by Warcraft945)
So I wanted to make a global leaderboard, but unfortunately I can’t get my dataStores from another script.
I have a script to manage all the dataStores, where I save and initialize the dataStores and another script to manage the global leaderboard.
How can I access my dataStores from another script or do I have to put in the global leaderboard in the same script (if it works like that)?
The DataStoresScript (in ServerScriptService):
-- << Services >> --
local DataStoreService = game:GetService("DataStoreService")
-- << Variables >> --
local data_MyOtherScores = DataStoreService:GetDataStore("MyOtherScores")
local data_Donations = DataStoreService:GetDataStore("DonationsScore")
local data_BetterTime = DataStoreService:GetDataStore("BetterTimeScore")
-- << Functions >> --
local function saveData(player)
local dataToSave = {
player.DataRepertory.LastTime.Value;
player.DataRepertory.WorseTime.Value
}
local donationsScore = player.DataRepertory.DonationScore.Value
local betterTime = player.DataRepertory.BetterTime.Value
local success, err = pcall(function()
data_MyOtherScores:SetAsync(player.UserId, dataToSave)
data_BetterTime:SetAsync(player.UserId, betterTime)
data_Donations:SetAsync(player.UserId, donationsScore)
end)
if success then
print("Data has been saved !")
else
print("Data hasn't been saved !")
warn(err)
end
end
-- << Connect >> --
game.Players.PlayerAdded:Connect(function (player)
-----Creating a folder for the data into the player
local dataFolder = Instance.new("Folder")
dataFolder.Name = "DataRepertory"
dataFolder.Parent = player
local BetterTime = Instance.new("NumberValue")
BetterTime.Name = "BetterTime"
BetterTime.Parent = dataFolder
local LastTime = Instance.new("NumberValue")
LastTime.Name = "LastTime"
LastTime.Parent = dataFolder
local WorseTime = Instance.new("NumberValue")
WorseTime.Name = "WorseTime"
WorseTime.Parent = dataFolder
local DonationScore = Instance.new("IntValue")
DonationScore.Name = "DonationScore"
DonationScore.Parent = dataFolder
local dataScores
local dataDonations
local dataBetterTime
local success, err = pcall(function ()
dataScores = data_MyOtherScores:GetAsync(player.UserId)
dataDonations = data_Donations:GetAsync(player.UserId)
dataBetterTime = data_BetterTime:GetAsync(player.UserId)
end)
wait(2) -- wait ajouté ici pour que le Menu fassent le changement des valeurs au début (sinon l'event .Changed n'est pas appellé au début)
if success then
LastTime.Value = dataScores[1]
WorseTime.Value = dataScores[2]
BetterTime.Value = dataBetterTime
DonationScore.Value = dataDonations
else
print("The player has not data !")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function ()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
warn(err)
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
warn(err)
end
end
end)
The globalLeaderboardScript (in ServerScriptService):
-- << Services >> --
local DataStoreService = game:GetService("DataStoreService")
-- << Variables >> --
local data_Donations = DataStoreService:GetOrderedDataStore("DonationsScore")
-- << Functions >> --
--If i don't set theses 3 test data my script doesn't print anything (probably because it don't get my data from the other script).
data_Donations:SetAsync("Mars", 19) -- temporary
data_Donations:SetAsync("Marss", 1058)-- temporary
data_Donations:SetAsync("Marssss", 234)-- temporary
local function updateLeaderboard()
local success, errorMessage = pcall(function()
local Data = data_Donations:GetSortedAsync(false, 10)
local topTEN = Data:GetCurrentPage()
for rank, data in ipairs(topTEN) do
local name = data.key
local score = data.value
print(name.." is ranked #"..rank.." with "..score.." score")
end
end)
if not success then
print(errorMessage)
end
end
-- << Connect >> --
while true do
--[[
for _, frame in pairs(game.Workspace.Levels.Level1.LeaderBoardBaseDonations.SurfaceGui.ImageLabel.Frame.Holder:GetChildren()) do
frame:Destroy()
end
]]--
updateLeaderboard()
print("Updated!")
wait(10) -- to change
end