Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Saving multiple variables in datastores?

Asked by 6 years ago

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?

0
You can create multiple datastores but a table would be the best way User#20388 0 — 6y
0
Hey man if I helped you out could you accept my answer? User#21908 42 — 5y
0
Lol I just realized you said thank you but did not accept my answer. User#21908 42 — 5y
0
if I helped you out please accept my answer. Otherwise comment how I can help you better User#21908 42 — 5y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
0
You should really be using pcall. User#21908 42 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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!

0
Thank you! Revisedy 23 — 6y

Answer this question