I recently made a datastore where i store 2 values in Strength and cash

Cash an strength perfectly show up when playing but when i close down the game and open it again it goes back to 0

https://gyazo.com/ca4be523dba905574192a5999a101d69

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("CashDataStore")
local ds2 = DataStore:GetDataStore("StrengthDataStore")

game.Players.PlayerAdded:Connect(function(player)	
	
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	
	local cash = Instance.new("IntValue",leader)
	cash.Name = "cash"
	
	local strength = Instance.new("IntValue",leader)
	strength.Name = "strength"
	
	strength.Value = ds2:GetAsync(player.UserId) or 0
	ds2:SetAsync(player.UserId, strength.Value)
	
	cash.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, cash.Value)

	cash.Changed:connect(function()
	ds1:SetAsync(player.UserId, cash.Value)
	end)
	
	strength.Changed:connect(function()
	ds2:SetAsync(player.UserId, strength.Value)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	ds1:SetAsync(player.UserId, player.leaderstats.cash.Value)
    ds2:SetAsync(player.UserId, player.leaderstats.strength.Value)
end)

3 comments

0

You should never use the changed event to save data in you have no control over when it saves and you can easily reach the data store limits for requests

0

Secondly you are using the same key for both values. This will overwrite the value. Use a table to save on requests.

0

Lastly you should look at how to manage data store errors using pcall to safely retry if things fail.