Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I use both Data Stores and the Classic leaderboard?

I wan't to make this sort of code work with a Data Store, this is so that I can check on values easily. How can I accomplish this?

   	 local leaderstats = Instance.new('Model')
    	leaderstats.Name = 'leaderstats'
    	leaderstats.Parent = player
 
	local wins = Instance.new('IntValue')
    	wins.Name = 'Wins'
   	 wins.Value = 0
   	 wins.Parent = leaderstats
	
	local credits = Instance.new('IntValue') 
	credits.Name = 'Mini-BUX'
	credits.Value = 0
	credits.Parent = leaderstats

2 answers

✓ Accepted answer

Datastores are very easy to set up, and once you understand them, they are incredibly useful for a whole manor of things.

It would be very easy to implement a datastore saving/loading system into your script.

To start, first you need to grab the datastore using the following code:

local Datastore = game:GetService("DataStoreService"):GetDataStore("SomeRandomName")

To Save data:

local Key = tostring(Player.userId) --So it is saved under the player's userid
local Value = 20 --We'll save 20 to the stores

Datastore:SetAsync(Key, Value) --As easy as that!

To Load data:

local Key = tostring(Player.userId) --Using the same key as before

local Value = Datastore:GetAsync(Key)

print(Value) --Should print 20

To implement this into your code, you will want something like this:

local Datastore = game:GetService("DataStoreService"):GetDataStore("Datastore1")

game.Players.PlayerAdded:connect(function(player)

 local leaderstats = Instance.new('Model')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player

local wins = Instance.new('IntValue')
    wins.Name = 'Wins'
 wins.Value = 0
 wins.Parent = leaderstats

local credits = Instance.new('IntValue') 
credits.Name = 'Mini-BUX'
credits.Value = 0
credits.Parent = leaderstats

local Wins = Datastore:GetAsync(tostring(player.userId)..":WINS")
local Credits = Datastore:GetAsync(tostring(player.userId)..":CREDS")

if Wins then wins.Value = Wins end
if Credits then credits.Value = Credits end

end)

game.Players.PlayerRemoving:connect(function(player)

local Stats = player:FindFirstChild("leaderstats")

if Stats then

local Wins = Stats:FindFirstChild("Wins")
local Creds = Stats:FindFirstChild("Mini-BUX")

if Wins then Datastore:SetAsync(tostring(player.userId)..":WINS", Wins.Value) end
if Creds then Datastore:SetAsync(tostring(player.userId)..":CREDS", Creds.Value) end

end

end)

Hope this helped (I wasn't really sure what you meant, but this is the most practical use of datastores regarding leaderstats), you might want to check for spelling mistakes etc.

Here's the wiki too if you need more help: Wiki page

1 comment

0

Sorry if I didn't make my question clear. Thank you darkelementallord for giving me a rough concept of data stores.

This discussion is preserved as it was. New answers, comments, and votes are closed.