DataStore isnt working on currency system

So I’ve created a currency system in my game, and made it so the data would save every time the player would leave the game.

But this isn’t the case, it doesn’t work and I am not sure why.

I’ve tried many things like enabling API services and it still will not work!

Any help would be awesome, as I really do not want to restart this work!

Thank you!

Here’s the currency and datastore lua

local DS = game:GetService("DataStoreService")
local cashStore = DS:GetDataStore("Mooneey")

local remote = game:GetService("ReplicatedStorage").CurrencyRemote.GiveCurrency

game.Players.PlayerAdded:Connect (function(player)
	
	local cashValue
	local success, err = pcall(function()
		cashValue = cashStore:GetAsync("Player_"..player.UserId
			
		)
	end)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	if success then
		Cash.Value = cashValue
	else
		print("Failed To Save Data, Sorry")
	end
end)

local function save (player)
	local success, err = pcall (function()
		cashStore:GetAsync("Player_"..player.UserId, player.leaderstats.Cash.Value)
	end)
	if success then
		print("Your Data Has Saved")
	else
		print("Failed To Save Data2, Sorry")
	end
end

local function autosave()
	while wait(10) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save (player)
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Cash.Value += amount
end)

spawn(autosave)
1 Like

are there any errors in the output?

1 Like

Nope, it just shows the data load prints

local DS = game:GetService("DataStoreService")
local cashStore = DS:GetDataStore("Mooneey")

local remote = game:GetService("ReplicatedStorage").CurrencyRemote.GiveCurrency

game.Players.PlayerAdded:Connect (function(player)
	
	local cashValue
	local success, err = pcall(function()
		cashValue = cashStore:GetAsync("Player_"..player.UserId
			
		)
	end)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	if success then
		Cash.Value = cashValue
	else
		print("Failed To Save Data, Sorry")
	end
end)

local function save (player)
	local success, err = pcall (function()
		cashStore:SetAsync("Player_"..player.UserId, player.leaderstats.Cash.Value)
	end)
	if success then
		print("Your Data Has Saved")
	else
		print("Failed To Save Data2, Sorry")
	end
end

local function autosave()
	while task.wait(10) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save (player)
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Cash.Value += amount
end)

task.spawn(autosave)

You were just using GetAsync in ‘save’, not SetAsync. I also changed a little bit of the code to be more ‘up-to-date’. :slightly_smiling_face:

2 Likes
cashStore:GetAsync("Player_"..player.UserId, player.leaderstats.Cash.Value)

Should be :SetAsync().

Cash.Value = cashValue
Should be Cash.Value = cashValue or 0.

3 Likes

Thank you guys so much for the help! you guys are the best

1 Like