Data store doesn't save

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.

I didn’t create any equivalents with it. What are you even talking about?

This is for an example.

local id = plr.UserId.."-cash"
local id2 = plr.UserId.."-victories"

local data
local data2

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

I’m not 100% sure if it’s correct.

Edit: I don’t have the programmer role for a reason.

Sorry for the delayed response, I was pretty busy and now I tested and this seems to work alright. I also added a few lines / changed, for simplicity.

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 or 0
	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("Saved")
	else
		print("Data hasn't been saved.")
		warn(err)		
	end
end)

Some things you should know:

  1. Client / Local scripts can’t have access to Data Store Service.
  2. It is much better to store the player data in one key, imo

I am sorry but, it is still a fail. No errors are found.

It didn’t seem to print the print Saved while I was testing, so I switched to server side and then removed the player from the game and seems like it is working and printing Saved. Not really sure why this is the behavior though.

And I am sure it is saving because I had changed the leaderstat value from the server side and on rejoining it works as expected.

Well, I’ve tested in my game instead studio. Guess what?

IT IS FINALLY WORKING. THANK YOU SO MUCH.

I will analyze the script carefully and try to recreate it without looking it, because of you helping me a very hard and long journey, you earned my respect of following you.

Once again, thank you!

1 Like

Also, i think the issue is because your datastore saves atleast every 30 seconds, zeus was pretty fast to setting his value and trying to exit the game then join back in to see if it saved, anyways you should create a coroutine thread and do something with it and whatever then it’ll repeat it. It explains a lot in this topic i sent earlier, you should read more.

Edit: nvm

This isn’t the error, the script does’t know what value to set to.

Ho sorry, I explained myself poorly, use both