Data store doesn't save

It is because earlier, your data structure was pretty different and it probably had saved data in that structure itself. It should work alright if you change the key for the DataStore.

local success, errormessage = pcall(function()
		data = ds:GetAsync(id)
	end)

Where is the second variant? If you added 2 variants in the playerRemoving function then you need to add the second one too.

Edit: In the error output, it said it attempted to index number with “Cash”, the number it was referring to is the player’s userId, and the “Cash” is the missing variant it couldn’t find.

So I just change the name of the key to something else like Cash2?

What do you mean by this? Please brighten me up more.

You need to add 1 more variant instead of just “id”.

local success, errormessage = pcall(function()
		data = ds:GetAsync(id, "missing variant")
	end)

What do you mean by missing variant?

No, I mean like the Datastore key that you use for referencing the Record of the player. Here is the edited script:

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 key = plr.UserId.."_Data"
	
	local data
	local success, errormessage = pcall(function()
		data = ds:GetAsync(key)
	end)
	
	if success then
		print("Successfully obtain data!")
		cash.Value = data.Cash
	else
		warn("Failed to load ata.")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local key = plr.UserId.."_Data"
	
	local data = {
		Cash = plr.leaderstas.Cash.Value;
	}
	
	local sucess, err = pcall(function()
		ds:SetAsync(key, data)
	end)
end)

Also there was a small error in the SetAsync, where you were passing the player object as the key, which wasn’t the same with what you were referencing in the GetAsync earlier.

I’m talking about arguments, here’s an example of a variant.

function die(variant, variant2)
    if variant then
       variant2.Health = 0
      end
end)

die(game.Workspace.reygenne1, game.Workspace.reygenne1.Humanoid)

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.