New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Would there be an easier way to use DataStore for multiple Values?

Asked by
danglt 189
1 year ago
Edited 1 year ago

I'm making a place that will save multiple values and I was wondering if I could use an easier way for values?

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(player)
 local folder = Instance.new("Folder",player)
 folder.Name = "places"
 local stat = Instance.new("IntValue",folder)
 stat.Name = "first"
 stat.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, stat.Value)
 stat.Changed:connect(function()
  ds:SetAsync(player.UserId, stat.Value)
 end)
end)


game.Players.PlayerRemoving:connect(function(player)
 ds:SetAsync(player.UserId, player.places.first.Value)
end)
0
use an array to store all the values User#23365 -5 — 1y
0
do a for i,v in pairs on the values parent Scarperino 20 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

You can use an array to save all your data:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(player)
 local d = ds:GetAsync(player.UserId)
 local folder = Instance.new("Folder",player)
 folder.Name = "places"
 local stat = Instance.new("IntValue",folder)
 stat.Name = "first"
 stat.Value = d[1] or 0
 ds:SetAsync(player.UserId, stat.Value)
 stat.Changed:connect(function()
  ds:SetAsync(player.UserId, {stat.Value; 'Hi lol';)
 end)
 print(d[2])
end)


game.Players.PlayerRemoving:connect(function(player)
 local tbl = {player.places.first.Value; 'Hi lol'}
 ds:SetAsync(player.UserId, player.places.first.Value)
end)
Ad

Answer this question