DataStore not retrieving data

I followed the official roblox Save Player Data Article ( Documentation - Roblox Creator Hub ) but for some reason it doesn’t work. I don’t get any errors. I’ve tried with prints and it confirms that the data is saved but when I join back it doesn’t see any saved data

local PlayerStatManager = {}

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

local sessionData = {}
local AUTOSAVE_INTERVAL = 60

function PlayerStatManager:ChangeStat(player,statName,value)
	local playerUserId = "Player_"..player.UserId
	assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
	if typeof(sessionData[playerUserId][statName]) == "number" then
		sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
		print(statName.." Value Added")
	else
		sessionData[playerUserId][statName] = value
		print("Value = Value")
	end
end

local function setupPlayerData(player)
	local playerUserId = "Player_"..player.UserId
	local success, data = pcall(function()
		local data = playerData:GetAsync(playerUserId)
	end)
	if success then
		if data then -- it says here that there is no data
			sessionData[playerUserId] = data
			print("Data Found")
		else
			sessionData[playerUserId] = {Coins=0, Stages=0, Level=0, Rebirths=0, Areas=1, Exp=0, MaxExp=10}
			print("No Data Found")
		end
	else
		warn("Cannot save data for player!")
	end
end

local function savePlayerData(playerUserId)
	if sessionData[playerUserId] then
		local tries = 0
		local success
		repeat
			tries = tries + 1
			success = pcall(function()
				playerData:SetAsync(playerUserId, sessionData[playerUserId])
			end)
			if not success then wait(1) end
		until tries == 3 or success
		if not success then
			warn("Cannot save data for player!")
		end
	end
end

local function saveOnExit(player)
	local playerUserId = "Player_"..player.UserId
	savePlayerData(playerUserId)
end

local function autoSave()
	while wait(AUTOSAVE_INTERVAL) do
		for playerUserId, data in pairs(sessionData) do
			savePlayerData(playerUserId)
		end
	end
end
spawn(autoSave)

game.Players.PlayerAdded:Connect(setupPlayerData)
game.Players.PlayerRemoving:Connect(saveOnExit)

return PlayerStatManager

1 Like

You need a check for current players, as PlayerAdded does not always work as intended.

1 Like

What do you mean with a check for current players?

for i,v in next, game.Players:GetPlayers()
 PlayerAdded(v)
end
game.Players.PlayerAdded:Connect(PlayerAdded)

I still don’t get it I guess I’m being dumb

In your snippet here, put the check for current players.

for i,v in next, game.Players:GetPlayers()
 PlayerAdded(v)
end

making it this

Test this in game, you may have turned off API services for studio or playeradded isn’t running for some reason.

PlayerAdded is running since it prints out that it didn’t find any data and I don’t have API services off and I tested it in-game aswell

I still don’t get why the playeradded worked fine. Everytime a player joined the code ran
edit: and it doesn’t work it keeps saying there is an error while loading the requested module script

Sometimes the first player joins before the PlayerAdded event is instanced and it won’t fire on the first player.

ModuleScript? This must be to do with something else unless…?

well the prints I put in the code has shown it does fire so that is not the problem

What are you trying to say? (here you can enjoy some random text since it needs to be longer)

Is there other scripts in the game or are you using the modulescript for datastore?

I’m using the module script for the datastore there is nothing else except for calling the ChangeStat function to change the values

Is the script you mentioned in the op the modulescript?

What do you mean with in the op? The DataStore script in this post is in a module script

Hmm, are there any errors?

Check if API is on.

There are no errors and API is on and I’ve even tested it in-game

I’m not sure on this, but I doubt datastores are best used in a modulescript.