DataStore problem

Hey!
I have a problem with my datastore. When i test the game it prints “Loaded data” and it prints “Saved data” throughout the game. But i still get the starter data.

local DS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local moneyStore = DS:GetDataStore("PlayerExperience")

local AUTOSAVE_INTERVAL = 60

-- Table to store each player's data during the current session

playerData = {}

local function saveData(dataStoreKey, value)
	local setSuccess, errorMessage = pcall(function()
		moneyStore:SetAsync(dataStoreKey, value)
	end)
	if not setSuccess then
		print(errorMessage)
	else
		print("Saved data")
	end
end

local function onPlayerRemoving(player)
	-- Update data store key
	local playerUserID = player.UserId
	if playerData[playerUserID] then
		saveData(playerUserID, playerData[playerUserID])
	end
end

local function onPlayerAdded(player)
	-- Initially get player's data from data store
	local playerUserID = player.UserId
	local success, storedData = pcall(function()
		return moneyStore:GetAsync(playerUserID)
	end)
	if success and storedData then
		print("Loaded data")
		playerData[playerUserID] = storedData
	else
		print("Loading new data")
		playerData[playerUserID] = {money = 0, level = 1} -- This is the data for new players. 
	end

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = playerData[playerUserID].money
	money.Parent = leaderstats

	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value =  playerData[playerUserID].level
	level.Parent = leaderstats

	wait(5)

	saveData(playerUserID,playerData[player.UserId])
end

coroutine.wrap(function()
	while wait(AUTOSAVE_INTERVAL) do
		print("Autosave")
		for i, player in pairs(Players:GetPlayers()) do
			if playerData[player.UserId] then
				saveData(player.UserId, playerData[player.UserId])
			end			
		end
	end
end)()

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

I don’t see anywhere, that you change the playerData table which is being saved.

1 Like

I had DataStore issues yesterday. I used the exact same thing I always used and I used a DataStore editor, it saved, but it wouldn’t load. I’m not sure why though.

did you even set the studio datastore saving

add this line of code in the end of script

game:BindToClose(function()
   wait(5)
end)

not sure if it work

Its not the saving. It’s the loading. If I use a datastore editor, it’s fine. In game, its not loading in.

its weird that you are saving the player data when the player joins shouldnt you loading them up?

in the playeradded event

im messing up here thinking its wrong post
i am loading on join

but your script shows your saving it on join


there

i didnt put my script
im just adding to this thread

thats not my post
that post was made by naketduderxii
check this image


yes i used snipping tool

	local success, storedData = pcall(function()
		return moneyStore:GetAsync(playerUserID)
	end)
	if success and storedData then
		print("Loaded data")
		playerData[playerUserID] = storedData
	else
		print("Loading new data")
		playerData[playerUserID] = {money = 0, level = 1} -- This is the data for new players. 
	end

I thought pcalls() were supposed to be written like this:

local success, err = pcall(function())

In which “err” means error. I believe your code is working fine - note that your if statement will only pass if both success and storedData is satisfied. If I read this code correctly, that is impossible because storedData is actually “err”, and you can’t have a success and a error at the same time.

Try doing something like this:

        local storedData = {}
        local success, err = pcall(function()
		storedData = moneyStore:GetAsync(playerUserID)
	end)
	if success and storedData ~= nil then
		print("Loaded data")
		playerData[playerUserID] = storedData
	else
		print("Loading new data")
		playerData[playerUserID] = {money = 0, level = 1} -- This is the data for new players. 
	end

His code is right. The variable storedData would be an error message if the success is false, otherwise it will be the value returned from function so no need to change it.

It loads the data, and then saves it again 5 second later. Thats because if the player leaves fast again, the data can sometimes be lost because of some glitch. So the save data on join fixes that.

Exactly, that shouldn’t be the problem.

your data load will never run because pcall returns success and failure meaning

	local success, storedData = pcall(function()
		return moneyStore:GetAsync(playerUserID)
	end)
	if success and storedData then
		print("Loaded data")
		playerData[playerUserID] = storedData

will never run

1 Like

I was about to say that. Yes. you (@naketduderxii) are checking if both the error and success are true. They cant be if you want it to work properly.

But it does print “Loaded data”. How would i then do it?

try taking off the storedData check in your “if” statement:

like this below:
if success then not if success and storedData then because storedData is the variable holding any errors that occur if any.