✓ 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)
local Value = 20
Datastore:SetAsync(Key, Value)
To Load data:
local Key = tostring(Player.userId)
local Value = Datastore:GetAsync(Key)
print(Value)
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
Sorry if I didn't make my question clear. Thank you darkelementallord for giving me a rough concept of data stores.