Data not Loading

I am currently in the middle of making an obby. I’m trying to save the player’s level that they left off on. However, being new to data stores, a confusing thing, leaves me with not achieving what I want to achieve, and I do not know why.

This is the script in ServerScriptService:

local event = game.ReplicatedStorage.DataSave
local dataStoreService = game:GetService("DataStoreService")
local levels = dataStoreService:GetDataStore("Level")
local level

event.OnServerEvent:Connect(function(player)
	local function updateLevel(currentLevel)
		local level = player.leaderstats.Level.Value
		return level
	end
	local success, data = pcall(function()
		return levels:GetAsync("Player_" .. player.UserId)
	end)
	if not data then
		levels:SetAsync("Player_" .. player.UserId, 1)
		print("data")
	else
		local updateSuccess, updateError = pcall(function()
			return levels:UpdateAsync("Player_" .. player.UserId, updateLevel)
		end)
		if updateSuccess then
			print("success")
		elseif updateError then
			print("error")
		end
	end
	if success then
		print("yay")
		print(success)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local success, data = pcall(function()
		local currentLevel = levels:GetAsync("Player_" .. player.UserId)
		level = currentLevel
		return levels:GetAsync("Player_" .. player.UserId)
	end)
	if data then
		local spawns = game.Workspace:GetDescendants()
		for index, respawn in pairs(spawns) do
			if respawn:IsA("SpawnLocation") then
				if respawn.Name == "Checkpoint" .. level - 1 then
					player.RespawnLocation = respawn
					player.Character:MoveTo(respawn.Positon)
				end
			end
		end
	end
end)

It does not return the player to where they left off, nor keep their “Level” value in their leaderstats.

  1. What do you want to achieve? I want to save the player’s level, so they can get back to where they left off.

  2. What is the issue? The player doesn’t go where they left off.

  3. What solutions have you tried so far? I read the Data Stores article, but it didn’t solve my issue.

Everything prints correctly, indicating that data does save, but I don’t think it loads data. I also checked the “Enable Studio Access to API Services” box, and still, nothing.

Did you try already printing the value that gets retrieved? Would be nice to know if it saves at least or not.

local event = game.ReplicatedStorage.DataSave
local dataStoreService = game:GetService("DataStoreService")
local levels = dataStoreService:GetDataStore("Level")
local level

event.OnServerEvent:Connect(function(player)
	local function updateLevel(currentLevel)
		local level = player.leaderstats.Level.Value
		print(level)
		return level
	end
	local success, data = pcall(function()
		return levels:GetAsync("Player_" .. player.UserId)
	end)
	if not data then
		levels:SetAsync("Player_" .. player.UserId, 1)
		print("data")
	else
		local updateSuccess, updateError = pcall(function()
			return levels:UpdateAsync("Player_" .. player.UserId, updateLevel)
		end)
		if updateSuccess then
			print("success")
		elseif updateError then
			print("error")
		end
	end
	if success then
		print("yay")
		print(success)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local success, data = pcall(function()
		local currentLevel = levels:GetAsync("Player_" .. player.UserId)
		level = currentLevel
		return levels:GetAsync("Player_" .. player.UserId)
	end)
	if data then
		print(level)
		local spawns = game.Workspace:GetDescendants()
		for index, respawn in pairs(spawns) do
			if respawn:IsA("SpawnLocation") then
				if respawn.Name == "Checkpoint" .. level - 1 then
					player.RespawnLocation = respawn
					player.Character:MoveTo(respawn.Positon)
				end
			end
		end
	end
end)

After adding two print() statements, printing the level variable, it always prints “1”.

When does OnServerEvent get called? When the Player leaves or when they reach a new checkpoint?
Scratch that, what script updates the Players leaderstats?

It indeed fires when the player reaches a new checkpoint.

Edit: A LocalScript updates the level value. Now that I write this, I should have the server change it and see what happens.

That’s the issue. A client is not authoritative to perform changes from anything the server owns.
Always change leaderstats with the Server (same for everything else unless the client should only see it).