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.
-
What do you want to achieve? I want to save the player’s level, so they can get back to where they left off.
-
What is the issue? The player doesn’t go where they left off.
-
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.