Datastore not working

I am trying to make a Datastore script, I’ve made only one working datastore, but can’t get this one to work
Nothing saves in this datastore when I tried it in studio and in-game, (I enabled API services.)
The Datastore errors here:

local success, data = pcall(function()
		return playerData:GetAsync(playerUserId, SessionData[playerUserId])
	end)
	if success then
		if data then
			SessionData = playerData[playerUserId]
		else
			playerData = {Cash = 0, Gems = 10, Level = 1, FoodItems = 0}
			print("h") ---- Prints H here,  when I rejoined after setting data on the server.
		end
	else
		warn("Error") 
	end

Datastore Script:

local module = {}
local playerData = game.DataStoreService:GetDataStore("Data")
local AUTO_INTERVAL_TIME = 60
local SessionData = {}


local function SessionCheck(player)
	
	local Folder = Instance.new("Folder", player)
	Folder.Name = "leaderstats"
	local Level = Instance.new("IntValue", Folder)
	Level.Name = "Level"
	local Gems = Instance.new("IntValue", Folder)
	Gems.Name = "Gems"
	local Cash = Instance.new("IntValue", Folder)
	Cash.Name = "Cash"
	local FoodItems = Instance.new("IntValue", Folder)
	FoodItems.Name = "Products"
	local playerUserId = player.UserId
	local success, data = pcall(function()
		return playerData:GetAsync(playerUserId, SessionData[playerUserId])
	end)
	if success then
		if data then
			SessionData = playerData[playerUserId]
		else
			playerData = {Cash = 0, Gems = 10, Level = 1, FoodItems = 0}
			
		end
	else
		warn("Error")
	end
	end
local function SaveData(PUID)
	if SessionData[PUID] then
		local tries = 0
		local success
		repeat
			tries = tries + 1
			success = pcall(function()
				playerData:SetASync(PUID, SessionData[PUID])
			end)
			if not success then wait(1) end
		until tries == 3 or success
		if not success then
			warn("Couldn't save data.")
			
		end
	end
end

function OnExit(plr)
	local playerUserID = plr.UserId
	SaveData(playerUserID)
end

local function updateInterval()
	while wait(AUTO_INTERVAL_TIME) do
		for playerUserId, data in pairs(SessionData) do
			SaveData(playerUserId)
		end
	end
end

spawn(updateInterval)
game.Players.PlayerAdded:Connect(SessionCheck)

game.Players.PlayerRemoving:Connect(OnExit)

2 Likes

Could you possibly provide a bit more information in the original post? What specific part of the data store is not working for example?

Sorry about not giving enough info, I’ve updated the post. Thanks for the help :slight_smile:

In the “SessionData” you aren’t saving any value.
Here:

	else
		playerData = {Cash = 0, Gems = 10, Level = 1, FoodItems = 0} 
		print("h") ---- Prints H here,  when I rejoined after setting data on the server.
	end

You can’t change the playerData without “:SetAsync()” so change the “SessionData” (example: SessionData[playerUserId] = {Cash = 0, Gems = 10, Level = 1, FoodItems = 0} )

Also, you aren’t saving the values of level, gems, cash or products, try like this:

if SessionData[PUID] then
	local plr = game.Players:GetPlayerByUserId(PUID)--Get Player By UserId
	
	for _, value in ipairs(plr:FindFirstChild("leaderstats"):GetChildren()) do-- Values
		local data = SessionData[PUID]
		
		data[value.Name] = value.Value--Find and set value
	end
	
	local data = SessionData[PUID]
	local tries = 0
	local success, err
	repeat
		success, err = pcall(function()
			playerData:SetAsync(PUID, SessionData[PUID])
		end)
		tries = tries + 1
		wait(1)
	until success or tries >= 3
	
	if success then
		print("Data Saved!") 
	else
		warn(err)
	end
	SessionData[plr] = nil --Delete SessionData in Server
else
	warn("Invalid ID!")
end

Sorry for my english, it is not very good. =)

I forgot to say that you are not loading the data either.

local function SessionCheck(player)
	local data
	
	local Folder = Instance.new("Folder", player)
	Folder.Name = "leaderstats"
	local Level = Instance.new("IntValue", Folder)
	Level.Name = "Level"
	local Gems = Instance.new("IntValue", Folder)
	Gems.Name = "Gems"
	local Cash = Instance.new("IntValue", Folder)
	Cash.Name = "Cash"
	local FoodItems = Instance.new("IntValue", Folder)
	FoodItems.Name = "Products"
	local playerUserId = player.UserId
	
	local success, err = pcall(function()
		data = playerData:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
			SessionData[playerUserId] = data 
		else
			SessionData[playerUserId] = {Cash = 0, Gems = 10, Level = 1, FoodItems = 0} --Set Default data
			print("New player") --Can remove
		end
	else
		warn(err)
	end
	
	local data = SessionData[playerUserId]
	Level.Value = data.Level
	Gems.Value = data.Gems
	Cash.Value = data.Cash
	FoodItems.Value = data.FoodItems
end

I’m new in the forum :smile:

I recommend to use Datastore2, a datastore module that uses cache to make a permanent and safe save system: How to use DataStore2 - Data Store caching and data loss prevention