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

Data Save is saving both values as the same when i rejoin?

Asked by
danglt 185
5 years ago

When i play a game one value is 10 and the other is 1, i rejoin and both of them are 10

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

game.Players.PlayerAdded:connect(function(player)
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
 local Money = Instance.new("IntValue",leader)
 Money.Name = "Money"
 Money.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, Money.Value)

 leader.Name = "leaderstats"
 local tot = Instance.new("IntValue",leader)
 tot.Name = "Total"
 tot.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, tot.Value)

 Money.Changed:connect(function()
  ds:SetAsync(player.UserId, Money.Value)

 tot.Changed:connect(function()
  ds:SetAsync(player.UserId, tot.Value)
end)
 end)
end)


game.Players.PlayerRemoving:connect(function(player)
 ds:SetAsync(player.UserId, player.leaderstats.Total.Value)
ds:SetAsync(player.UserId, player.leaderstats.Money.Value)
end)

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

It's because when you leave, you're saving the "Money" value to the same table key as the "Total" value, effectively overwriting it.

I'm not a master at datastores, but I think you could do:

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

game.Players.PlayerAdded:connect(function(player)
 local data = ds:GetAsync(player.UserId) -- get the player's own datastore
 if data == nil then
  data = {Total = 0, Money = 0} -- set all data to nothing
 end
 datas[player.UserId] = data
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
 local Money = Instance.new("IntValue",leader)
 Money.Name = "Money"
 Money.Value = data.Money or 0
 ds:SetAsync(data.Money, Money.Value)

 local tot = Instance.new("IntValue",leader) --remove leader.Name, you've already defined it
 tot.Name = "Total"
 tot.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(data.Total, tot.Value)

 Money.Changed:connect(function()
  ds:SetAsync(data.Money, Money.Value)
  datas[player.UserId].Total = tot.Value -- set the current data
 end) -- add end)?

 tot.Changed:connect(function()
  ds:SetAsync(data.Total, tot.Value)
  datas[player.UserId].Total = tot.Value -- set the current data
 end) -- remove end)?
end)


game.Players.PlayerRemoving:connect(function(player)
 local data = datas[player.UserId] -- get the player's current data
 ds:SetAsync(data.Total, player.leaderstats.Total.Value)
 ds:SetAsync(data.Money, player.leaderstats.Money.Value)
end)

I think this will work? I'm not really sure. The main thing tho is that you should have different places for different values. (You should also make it so that it only saves when the player leaves)

Ad

Answer this question