I get how to save and load one variable like for example Money in leaderstats, but would I have to make a table to load multiple? For example, Money and Kills in leaderstats?
This will load in when the player loads in
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CashSaveSystem") game.Players.PlayerAdded:connect(function(player) local Leader = Instance.new("Folder",player) --Creates Leaderstas Leader.Name = "leaderstats" local Cash = Instance.new("IntValue",Leader) --Creats Cash Cash.Name = "Cash" Cash.Value = ds:GetAsync(player.UserId) or 0 --Starter Money ds:SetAsync(player.UserId, Cash.Value) --Saving Cash Cash.Changed:connect(function() ds:SetAsync(player.UserId, Cash.Value) --Saves when value changes end) end) game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player.leaderstats.Cash.Value) --Saves when player Leaves end)
FOr Multiple ones do Diffrent DS so ds becomes ds1 you can do this by hitting ctrl+h and put the word in
local DataStore = game:GetService("DataStoreService") local ds1 = DataStore:GetDataStore("KillsSaveSystem") game.Players.PlayerAdded:connect(function(player) local Leader = player:WaitForChild("leaderstats") Leader.Name = "leaderstats" local Kills = Instance.new("IntValue",Leader) Kills.Name = "Kills" Kills.Value = ds1:GetAsync(player.UserId) or 0 ds1:SetAsync(player.UserId, Kills.Value) Kills.Changed:connect(function() ds1:SetAsync(player.UserId, Kills.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds1:SetAsync(player.UserId, player.leaderstats.Kills.Value) end)
You could do this:
local DSS = game:GetService("DataStoreService") local moneyDataStore = DSS:GetDataStore("ExampleDataStore", money) local killsDataStore = DSS:GetDataStore("ExampleDataStore", kills) local winsDataStore = DSS:GetDataStore("ExampleDataStore", wins) --and so on
you can name the data store whatever you like and you can name the second word whatever you want as long as you are consistent throughout your scripts. Then when you want to load the data you can do something like this:
game.Players.PlayerAdded:Connect(function(plr) -- your leaderboard stuff here local uniqueKey = "User_"..plr local success, message = pcall(function() cash.Value = moneyDataStore:GetAsync(uniqueKey) kills.Value = killsDataStore:GetAsync(uniqueKey) wins.Value = winsDataStore:GetAsync(uniqueKey) end) if success then -- code here else -- more code here end end)
Hope this helped. Have a great day scripting!