Why isn't my datastore saving?

Script

local dss = game:GetService("DataStoreService")
local ds1 = dss:GetDataStore("rap")
local ds2 = dss:GetDataStore("inv")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder",plr)
	stats.Name = "leaderstats"
	
	local inv = Instance.new("Folder",game.ReplicatedStorage.Inventories)
	inv.Name = plr.Name.."Inventory"
	
	local rap = Instance.new("IntValue",stats)
	rap.Name = "RAP"
	rap.Value = ds1:GetAsync(plr.UserId) or 0
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local stats = plr:FindFirstChild("leaderstats")
	local inv = plr:FindFirstChild(plr.Name.."Inventory")
	
	if stats and inv then
		pcall(function()
			ds1:SetAsync(plr.UserId,stats.RAP.Value)
		end)
	end
end)

game:BindToClose(function()
	for _,plr in pairs(game.Players:GetChildren()) do
		local stats = plr.leaderstats
		local RAP = stats.RAP

		pcall(function()
			ds1:SetAsync(plr.UserId,RAP.Value)
		end)
	end
end)

For some reason it doesn’t save when I rejoin, and I used an answer to the problem from a previous post, and it worked the time then, but now it doesn’t work. The post was only made 11 days ago.

3 Likes

Ah, i see, it is because player leaves too early, script cant get time to read what player had, try making player data in one table and make client send that data upon leaving game

That’s quite confusing to think about, can you give a code example?

problem is… i dont have it… im still working on this, i was facing same problem, and still i am…

Maybe some smart guy will come and tell us how to do it.

wait, hold on, are you testing in studio? try to test in live game

Bruh, when I go into studio, literally nothing works anymore, everything is wrecked. I used no freemodels or plugins on this account.

Virus maybe? Who knows at this point…

Okay, I fixed it. Yet I did mention there was no plugins installed on the account being used for this, and no models. But it doesn’t work in-game either.

I’d recommend encasing sanity checks for your pcalls in case if your data doesn’t load/save, that way you can easily find where the error could lie

I do also recommend looking at this, cause your script is a bit wonky on how it functions:

1 Like

I was so stupid… Thank you for this.

So I used the entire script and customized it, and yet now my leaderstats doesn’t even pop up.

Maybe this works?

local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local ID = currencyName.."-"..player.UserId
	local savedData = nil	
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		currency.Value = savedData
		print("Data loaded")
	else
		currency.Value = 0
		print("New player")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("Error")
		end
	end
	wait(5)	
end)```
2 Likes

I suggest saving during a bindtoclose as well. (Just copy and paste the code from player.playerromoving into the kick loop)

2 Likes