I don't know why, but this doesn't seem to be working?
local DS = game:GetService("DataStoreService"):GetDataStore("Save") game.Players.PlayerAdded:connect(function(plr) local stats = Instance.new("IntValue", plr) stats.Name = "leaderstats" local cash = Instance.new("IntValue", stats) cash.Name = "Money" local job = Instance.new("StringValue", stats) job.Name = "Job" local key = "plr_"..plr.UserId if DS:GetAsync(key) then local stats = DS:GetAsync() cash.Value = stats[1] job.Value = stats[2] else cash.Value = 0 job.Value = "None" end end) game.Players.PlayerRemoving:connect(function(plr) local stats = plr.leaderstats local cash = stats.Money local job = stats.Job local key = "plr_"..plr.UserId DS:SetAsync(key, cash.Value, job.Value) end)
When I rejoib, my money is "0" and my job is ""
You have a very tiny error on line 11: you haven't provided a key to GetAsync
. Additionally, you're incorrectly saving the two leaderboard stats on line 24.
Also, rather than calling GetAsync
twice, it's better to store the result of the first call, and then check if it exists:
--The 'or nil' is necessary here: https://scriptinghelpers.org/questions/461/ local stats = DS:GetAsync(key) or nil if stats then cash.Value = stats[1] job.Value = stats[2] else --... end
Alternatively, you can reduce that if
into two lines:
local stats= DS:GetAsync(key) or nil cash.Value = stats and stats[1] or 0 job.Value = stats and stats[2] or "None"
SetAsync
only takes one Value after the provided Key, not multiple. Plus, you're using the return like it's a Table anyway:
DS:SetAsync(key, {cash.Value, job.Value}) --Notice the curly braces.
There are two problems with your code :-
local key = "plr_"..plr.UserId if DS:GetAsync(key) then -- you check the ds here but don't use the result this is a waste of requests local stats = DS:GetAsync() -- you have not given the key cash.Value = stats[1] job.Value = stats[2] else cash.Value = 0 job.Value = "None" end game.Players.PlayerRemoving:connect(function(plr) local stats = plr.leaderstats local cash = stats.Money local job = stats.Job local key = "plr_"..plr.UserId DS:SetAsync(key, cash.Value, job.Value) -- when saving multiple data it needs to be as a table end)
Saving multiple values using a Table
The final result:-
local DS = game:GetService("DataStoreService"):GetDataStore("Save") game.Players.PlayerAdded:connect(function(plr) local stats = Instance.new("IntValue", plr) stats.Name = "leaderstats" local cash = Instance.new("IntValue", stats) cash.Name = "Money" local job = Instance.new("StringValue", stats) job.Name = "Job" local key = "plr_"..plr.UserId -- make a variable of the data once local stats = DS:GetAsync(key) if stats then cash.Value = stats[1] job.Value = stats[2] else cash.Value = 0 job.Value = "None" end end) game.Players.PlayerRemoving:connect(function(plr) local stats = plr.leaderstats local cash = stats.Money local job = stats.Job local key = "plr_"..plr.UserId DS:SetAsync(key, {cash.Value, job.Value}) -- this Is now in a table format end)
Hope this helps.