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?
✓ ANSWER ACCEPTED
Saving multiple variables in datastores?
2 answers
✓ Accepted answer
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)
1 comment
0
You should really be using pcall.
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!
1 comment
0
Thank you!
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
You can create multiple datastores but a table would be the best way
Hey man if I helped you out could you accept my answer?
Lol I just realized you said thank you but did not accept my answer.
if I helped you out please accept my answer. Otherwise comment how I can help you better