DataStoresService - Problem for sharing DataStores among differents script

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
1 Like

I’m not sure if I understood you correctly (you want to access variables related to data store from DataStoresScript to globalLeaderboardScript). You can use in globalLeaderboardScript BindableFunction(s) to get the data store variables from DataStoresScript to this script.

2 Likes

Hi thanks for the answer, yes you correctly understand, but the only problem using this methods, it that i will not be able to get All the player for my leaderboard na ? I will just be able to get the player in the current server ? then my leaderboard will not be interServer ?

Is your problem the fact that you can’t get leaderboard data of players not currently playing on the game’s servers?

GlobalDataStores and OrderedDataStores are different you’ll have to use OrderedDatastore for both.

1 Like

No my problem is that in my other script (leaderboard) i can’t get the dataStores. (DataStores that are previously saved in an other script).

If you want data to be accessible from different scripts, you could use a module script and use DBMS concepts to get and modify the data from individual players by storing them in a table.

Database - Wikipedia

1 Like

Okk then what do i need to do ?
Change which line in my scripts ?
Like do i need to change this

local data_Donations = DataStoreService:GetDataStore("DonationsScore")

for

local data_Donations = DataStoreService:GetOrderedDataStore("DonationsScore")

Correct, that is what you should do.

It seems to work, i will try to do it clear, like the script and leaderboard, and i come back if i have a problem.

A other question, can i like “delete” some data for a key, cuz for my test i assigned value to 3 key. but can i delete them ?

Try setting the data to erase to nil.

1 Like

Use RemoveAsync on the key in the datastore.

2 Likes

Whoua Thanks a lot dude x).
You solve a lot of my problem, mainly the dataStores for leaderboard. (But that’s a huge one)

1 Like