I am making a datastore that stores gems and dollars. When I run the script I get no error I only get "Success, the currency[1] is 56". It is meant to print the value but for some reason on line 23 and 24 the currency[1] and [2] don't change to the gems or dollars value that are in the game. It only saves when I change the values of currency[1] or [2] specifically with the exact number and no references.

local currency = {}
local dss = game:GetService("DataStoreService")
local currencyData = dss:GetDataStore("currency")
game:GetService("Players").PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name =  "leaderstats"
	
	local dollars = Instance.new("IntValue",leaderstats)
	dollars.Name = "Dollars"
	
	local gems = Instance.new("IntValue",leaderstats)
	gems.Name = "Gems"
	
	local success,err = pcall(function()
	 currency = currencyData:GetAsync(plr.UserId.."-Currency") or {0,0}
	end)
	
	dollars.Value = currency[1]
	gems.Value = currency[2]
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)	
	currency[1] = plr.leaderstats.Dollars.Value
	currency[2] = plr.leaderstats.Gems.Value
		
	local success,err = pcall(function()
		currencyData:SetAsync(plr.UserId.."-Currency",currency)
	end)
	
	if success then
		print ("success, currency[1] is "..currency[1])
	end
end)


2 comments

0

By any chance are you modifying gems/dollars using LocalScripts? If so, their changes won't replicate to the server, meaning the player will see their gems/dollars changing but the server will still see the old values. Also note that you should handle the case when GetAsync fails (and currency will remain unchanged and refer to the data of whichever player loaded/saved last)

0

This is in a regular script to but thanks for trying.