Why did my datastore suddently got lost

Today i entered roblox studio to fix some small bugs and add something new . But i notice something was off some of my datastore are missing and the game doesn’t find them and i searched the datastore and saw that they aren’t deleted can someone give me any help??

1 Like

I would first check to make sure Studio still has access to Data Stores by making sure it’s enabled:

  1. Open Studio’s File ⟩ Game Settings window.

  2. Navigate to Security.

  3. Enable the Enable Studio Access to API Services toggle.

  4. Click Save.

If it is already enabled are you using any DataStore wrappers that add session locking or anything like that?

1 Like

yeah I also noticed some of my datastore keys being deleted for no reason so I don’t know what that’s about.

It’s enabled some users have their wins and some got theirs deleted

Could you share a sample of the DataStore code so I can see what could be going wrong?

Sorry for late reply heres the code

local Players = game:GetService(“Players”)
local SafeDataStore = require(game.ServerScriptService.SafeDataStore)

local DATASTORE_NAME = “PlayerWinsData” – << IL TUO NOME
local KEY_PREFIX = “Wins_”
local MAX_RETRIES = 3

local function getDataStore()
return SafeDataStore:GetDataStore(DATASTORE_NAME)
end

local function loadStats(player)
local ds = getDataStore()
local key = KEY_PREFIX .. player.UserId

local data
local success, result

for attempt = 1, MAX_RETRIES do
	success, result = pcall(function()
		return ds:GetAsync(key)
	end)

	if success then
		data = result
		break
	else
		warn("GetAsync failed:", result)
		task.wait(1)
	end
end

-- Se non esiste, crea data nuova
if type(data) ~= "table" then
	data = { Wins = 0 }
end

return data

end

local function saveStats(player)
local leaderstats = player:FindFirstChild(“leaderstats”)
if not leaderstats then return end

local ds = getDataStore()
local key = KEY_PREFIX .. player.UserId

local dataToSave = {
	Wins = leaderstats.Wins.Value
}

for attempt = 1, MAX_RETRIES do
	local success, err = pcall(function()
		ds:SetAsync(key, dataToSave)
	end)

	if success then
		return
	else
		warn("SetAsync failed:", err)
		task.wait(1)
	end
end

end

local function setupLeaderstats(player)
local data = loadStats(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = data.Wins or 0
wins.Parent = leaderstats

end

Players.PlayerAdded:Connect(function(player)
setupLeaderstats(player)
end)

Players.PlayerRemoving:Connect(function(player)
saveStats(player)
end)

game:BindToClose(function()
for _, player in ipairs(Players:GetPlayers()) do
saveStats(player)
end
end)

I have found one potentially leading issue in your script. You do not use regular quotes. ". You can’t use smart quotes in a Lua script. Try that as a solution first, and let me know if that works!

i have the same issue again with a different script without quotes

local Players = game:GetService(“Players”)
local DataStoreService = game:GetService(“DataStoreService”)
local RunService = game:GetService(“RunService”)

local winsDataStore = DataStoreService:GetDataStore(“PlayerWinsData”)

local function setupLeaderstats(player)
local leaderstats = player:FindFirstChild(“leaderstats”)
if leaderstats and not leaderstats:IsA(“Folder”) then
warn(“LeaderboardSetup: Existing ‘leaderstats’ for " .. player.Name .. " is not a Folder. Removing and recreating.”)
leaderstats:Destroy()
leaderstats = nil
end

if not leaderstats then
    leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
end

local loadedWins = 0
local key = "Wins_" .. player.UserId

local success, data = pcall(function()
    return winsDataStore:GetAsync(key)
end)

if success and data ~= nil then
    loadedWins = tonumber(data) or 0
elseif not success then
    warn("LeaderboardSetup: Error loading wins for " .. player.Name .. " (UserId: " .. player.UserId .. "): " .. tostring(data))
end

local winsStat = leaderstats:FindFirstChild("Wins")
if not winsStat or not winsStat:IsA("IntValue") then
    if winsStat then
        winsStat:Destroy()
    end
    winsStat = Instance.new("IntValue")
    winsStat.Name = "Wins"
    winsStat.Parent = leaderstats
end
winsStat.Value = loadedWins

end

local function savePlayerWins(player)
local leaderstats = player:FindFirstChild(“leaderstats”)
if leaderstats then
local winsStat = leaderstats:FindFirstChild(“Wins”)
if winsStat and winsStat:IsA(“IntValue”) then
local key = “Wins_” .. player.UserId
local tries = 0
local success, err
repeat
tries = tries + 1
success, err = pcall(function()
winsDataStore:SetAsync(key, winsStat.Value)
end)
if not success then
warn("LeaderboardSetup: Error saving wins for " .. player.Name .. " (UserId: " .. player.UserId .. "): " .. tostring(err) .. " (Attempt " .. tries .. “)”)
task.wait(1)
end
until success or tries >= 3
return success
end
end
return false
end

Players.PlayerAdded:Connect(setupLeaderstats)
Players.PlayerRemoving:Connect(savePlayerWins)

for _, player in Players:GetPlayers() do
setupLeaderstats(player)
end

game:BindToClose(function()
if RunService:IsStudio() then
– In Studio, PlayerRemoving is usually enough
return
end
local players = Players:GetPlayers()
for _, player in players do
savePlayerWins(player)
end
if players > 0 then
task.wait(3)
end
end)

Nevermind i actually manage to limitate the damages changing the script only 2 account lost their progress and one of them was me so thanks

1 Like

Also, an addition, the unfamiliar DataStore thing you used is not something that I would recommend. Consider using Roblox’s own DataStore service.

Edit: You are still using “”. You can not use those; you must use "" instead.

1 Like