Why isn't the money saving but everything else is?

I’m trying to make a saving leaderstats script (but I dont want it to show so I just put the folder name to “values”) and everything is working except the cash?

local dss = game:GetService("DataStoreService")
local datastore = dss:GetDataStore("CashStorage")
local datastore2 = dss:GetDataStore("BottlesStorage")
local datastore3 = dss:GetDataStore("ApplesStorage")
local datastore4 = dss:GetDataStore("OrangeStorage")
local datastore5 = dss:GetDataStore("FishStorage")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "values"
end)

game.Players.PlayerAdded:Connect(function(plr)
	local folder = plr:WaitForChild("values")

	local Cash = Instance.new("NumberValue", folder)
	Cash.Name = "Cash"
	Cash.Value = datastore:GetAsync(plr.UserId)

	while true do
		wait(120)
		datastore:SetAsync(plr.UserId, Cash.Value)
	end
end)


game.Players.PlayerAdded:Connect(function(plr)
	local folder = plr:WaitForChild("values")

	local Bottles = Instance.new("NumberValue", folder)
	Bottles.Name = "Bottles"
	Bottles.Value = datastore2:GetAsync(plr.UserId)

	while true do
		wait(120)
		datastore2:SetAsync(plr.UserId, Bottles.Value)
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local folder = plr:WaitForChild("values")

	local Apples = Instance.new("NumberValue", folder)
	Apples.Name = "Apples"
	Apples.Value = datastore3:GetAsync(plr.UserId)

	while true do
		wait(120)
		datastore3:SetAsync(plr.UserId, Apples.Value)
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local folder = plr:WaitForChild("values")

	local Oranges = Instance.new("NumberValue", folder)
	Oranges.Name = "Oranges"
	Oranges.Value = datastore4:GetAsync(plr.UserId)

	while true do
		wait(120)
		datastore4:SetAsync(plr.UserId, Oranges.Value)
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local folder = plr:WaitForChild("values")

	local Fish = Instance.new("NumberValue", folder)
	Fish.Name = "Fish"
	Fish.Value = datastore5:GetAsync(plr.UserId)

	while true do
		wait(120)
		datastore5:SetAsync(plr.UserId, Fish.Value)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local folder = plr:FindFirstChild("values")
	folder.Name = "values"

	local Cash = folder:FindFirstChild("Cash")
	
	datastore:SetAsync(plr.UserId, Cash.Value)

	local Bottles = folder:FindFirstChild("Bottles")

	datastore2:SetAsync(plr.UserId, Bottles.Value)
	
	local Apples = folder:FindFirstChild("Apples")

	datastore3:SetAsync(plr.UserId, Apples.Value)
	
	local Oranges = folder:FindFirstChild("Oranges")
	
	datastore4:SetAsync(plr.UserId, Oranges.Value)
	
	local Fish = folder:FindFirstChild("Fish")
	
	datastore5:SetAsync(plr.UserId, Fish.Value)
end)

Thanks!

Is the cash value the only one here that is being changed on the client? The server may not be saving the correct value because the change on the client won’t replicate to the server.

1 Like

oooh I think that might be the problem! Thanks for the help!

1 Like

You should be using a table value to store your stats within a single ‘DataStore’ instance. Your current approach is going to frequently encounter the limitations of the ‘DataStoreService’.