Data store doesn't save

I don’t think you are making any sense, GetAsync accepts only the key you want to get the data for.

ok i’ll do some research my bad

1 Like

So is this script fully correct now?

local DSS = game:GetService("DataStoreService")
local ds = DSS:GetDataStore("CashAndWins")
local run = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	
	local cash = Instance.new("IntValue",folder)
	cash.Name = "Cash2"
	
	local id = plr.UserId.."_Data"
	
	local data
	local success, errormessage = pcall(function()
		data = ds:GetAsync(id)
	end)
	
	if success then
		print("Successfully obtain data!")
		cash.Value = data.Cash2
	else
		warn("Failed to load data.")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local id = plr.UserId.."_Data"
	
	local data = {
		Cash2 = plr.leaderstats.Cash2.Value;
	}
	
	local sucess, err = pcall(function()
		ds:SetAsync(id,data)
	end)
	
	if success then
		print("data saved")
	end
end)

Also why did you get the RunService?

Try it, who knows it might work or not.

You don’t need to rename the cash stat to Cash2 though, it should work.

Although there is a typo mistake in your script in the end, where you referenced success but you named the variable sucess

Also, there’s still gonna problems even if the script is perfectly fine, the server the user is in may close for some reasons and it won’t count as a playerRemoving, you could use “BindToClose” where if the game closes, it will fire a function that’ll save some user’s data and their stats. Although i’m a bit lazy this topic could help you with that.

Nope, it still fails. I didn’t print out data saved.

Any errors in the console or anything else we can use to trace it?

It doesn’t show an error. All it loads is data loaded successfully.

local DSS = game:GetService("DataStoreService")
local ds = DSS:GetDataStore("CashAndWins")
local run = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	
	local cash = Instance.new("IntValue",folder)
	cash.Name = "Cash"
	
	local id = plr.UserId.."_Data"
	
	local data
	local success, errormessage = pcall(function()
		data = ds:GetAsync(id)
	end)
	
	if success then
		print("Successfully obtain data!")
		cash.Value = data.Cash
	else
		warn("Failed to load data.")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local id = plr.UserId.."_Data"
	
	local data = {
		Cash = plr.leaderstats.Cash.Value;
	}
	
	local success, err = pcall(function()
		ds:SetAsync(id,data)
	end)
	
	if success then
		print("data saved")
	else
		print("failed")
	end
end)

You can simply just write the amount of gold that the player had instead of making a index table of a cash that saved the player’s values, i don’t even know how to fix your problem.

Just realized, is that even a normal script or is it localscript? Place that script inside the serverscriptservice for a bit of safety.

This is a server script that is under ServerScriptService, duh.

Well, try the other advise i gave you.

I already said this is in ServerScriptService.

I’m referring to this, not the one about the serverscript location and it’s type.

Your not making any sense, do you mean make a starting value of the cash and give it to whoever is new to the game?

Erase that, replace the setAsync function inside the playerRemoving with this.

local success, err = pcall(function()
		ds:SetAsync(id, plr.leaderstats.Cash.Value)
	end)

Replace the one inside the playerAdded with this.

local success, errormessage = pcall(function()
		data = ds:GetAsync(id)
	end)
	
	if success then
		print("Successfully obtain data!")
		cash.Value = data
	else
		warn("Failed to load data.")
	end

Just try it, it’s not like i’m in studio and i’m like “mhm, this’ll 100% work!”

No, I am gonna store multiple datas with it, so I am creating a table with it.

Zeus, you could make different equivalents of the data variable in the playerAdded and in the playerRemoving functions.