Help! Data Store Saving But Not Loading

Hello, my data store prints “Data Saved” whenever I leave the game but whenever I join back my data does not load.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local DataStore = DataStoreService:GetDataStore("Production") -- Always set to "Offical" When Publishing

local function givePlayerCurrency(player: player)
	while true do
		task.wait(1)
		player.leaderstats.Food.Value += 1
		player.leaderstats.Coins.Value += 1
	end
end

local function setupPlayerData(player: player)
	local userID = player.UserId
	local key = "Player_"..userID
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local food = Instance.new("IntValue", leaderstats)
	food.Name = "Food"
	food.Value = 0
	
	local coins = Instance.new("IntValue", leaderstats)
	coins.Name = "Coins"
	coins.Value = 0
	
	local success, returnValue
	success, returnValue = pcall(DataStore.GetAsync, DataStore, key)
	
	if success then
		if returnValue == nil then
			returnValue = {
				Food = 0,
				Coins = 0,
			}
		end
		
		print(returnValue)
		food.Value = if returnValue.Food ~= nil then returnValue.Food else 0
		coins.Value = if returnValue.Coins ~= nil then returnValue.Coins else 0
		
	else
		player:Kick("Error Loading Data, Try Again Later. If Issue Pursues Contact Us!")
		print(player.Name.." Data Loading Error!!!")
	end
	--givePlayerCurrency(player)
end

local function save(player)
	local userID = player.UserId
	local key = "Player_"..userID
	
	local food = player.leaderstats.Food.Value
	local coins = player.leaderstats.Coins.Value
	
	local dataTable = {
		food = food, 
		coins = coins,
	}
	print(dataTable)
	
	local success, returnValue
	success, returnValue = pcall(DataStore.UpdateAsync, DataStore, key, function()
		return dataTable
	end)
	
	if success then
		print("Data Saved")
	else
		print("Data Saving Error!!!")
	end
end

local function onShutdown()
	if RunService:IsStudio() then
		task.wait(2)
	else
		local finished = Instance.new("BindableEvent")
		local allPlayers = Players:GetPlayers()
		local leftPlayers = #allPlayers
		
		for _, player in ipairs(allPlayers) do
			coroutine.wrap(function()
				save(player)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)()
		end
		finished.Event:Wait()
	end
end

Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)

This line is kind of weird:

success, returnValue = pcall(DataStore.GetAsync, DataStore, key)

The error is the wrong usage of :GetAsync. It’s neither a peoperty nor a child of DataStore, it’s a function.

You also later use UpdateAsync incorrectly.

Read this again and it should help you a lot:

https://create.roblox.com/docs/cloud-services/datastores

You are saving food and coins but loading Food and Coins. They should have the same capital characters.

Try this:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local DataStore = DataStoreService:GetDataStore("Production") -- Always set to "Offical" When Publishing

local function givePlayerCurrency(player: player)
	while true do
		task.wait(1)
		player.leaderstats.Food.Value += 1
		player.leaderstats.Coins.Value += 1
	end
end

local function setupPlayerData(player: player)
	local userID = player.UserId
	local key = "Player_"..userID
	
	local leaderstats = Instance.new"Folder"
	leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
	
	local food = Instance.new"IntValue"
	food.Name = "Food"
    food.Parent = leaderstats
	
	local coins = Instance.new"IntValue"
	coins.Name = "Coins"
    coins.Parent = leaderstats
	
	local success, returnValue = pcall(DataStore.GetAsync, DataStore, key)
	
	if success then
		print(returnValue)
		if returnValue then
           food.Value = returnValue.food
           coins.Value  = returnValue.coins
        end
	else
		player:Kick("Error Loading Data, Try Again Later. If Issue Pursues Contact Us!")
		print(player.Name.." Data Loading Error!!!")
	end
	--givePlayerCurrency(player)
end

local function save(player)
	local userID = player.UserId
	local key = "Player_"..userID
	
	local food = player.leaderstats.Food.Value
	local coins = player.leaderstats.Coins.Value
	
	local dataTable = {
		food = food, 
		coins = coins,
	}
	print(dataTable)
	
	local success, returnValue
    success, returnValue = pcall(DataStore.UpdateAsync, DataStore, key, function()
		return dataTable
	end)
	
	if success then
		print("Data Saved")
	else
		print("Data Saving Error!!!")
	end
end

local function onShutdown()
	if RunService:IsStudio() then
		task.wait(2)
	else
		local finished = Instance.new("BindableEvent")
		local allPlayers = Players:GetPlayers()
		local leftPlayers = #allPlayers
		
		for _, player in ipairs(allPlayers) do
			coroutine.wrap(function()
				save(player)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)()
		end
		finished.Event:Wait()
	end
end

Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)

Edit: I noticed that you weren’t using UpdateAsync correctly as well so change:

local success, returnValue
    success, returnValue = pcall(DataStore.UpdateAsync, DataStore, key, function()
		return dataTable
	end)

to:

local success, returnValue
    success, returnValue = pcall(DataStore.UpdateAsync, DataStore, key, function(dataTable)
		return dataTable
	end)

Hello! I changed to profile service.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.