Sometime all data is loss. I want to know how fixed pls help
in output says DataStoreService: AccessForbidden: Access forbidden. API: GetAsync, Data Store: DataSave
local RunService = game:GetService(“RunService”)
local DataStoreService = game:GetService(“DataStoreService”)local DataSave = DataStoreService:GetDataStore(“DataSave”)
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new(“Folder”, plr)
stats.Name = “Data”local function createIntValue(name, value, parent)
local intValue = Instance.new(“IntValue”)
intValue.Name = name
intValue.Value = value
intValue.Parent = parent
return intValue
endlocal dataValues = {
{“Cash”, 0}, {“Gem”, 0},
}for _, data in pairs(dataValues) do
createIntValue(data[1], data[2], stats)
endlocal function loadData()
local success, data = pcall(function()
return DataSave:GetAsync(plr.UserId)
end)
if success and data then
for name, value in pairs(data) do
if stats:FindFirstChild(name) then
stats[name].Value = value
end
end
end
endloadData()
local issave = false
local delayTime = 5local function saveData()
if not issave then
issave = true
local dataToSave = {}
for _, value in pairs(stats:GetChildren()) do
if value:IsA(“IntValue”) then
dataToSave[value.Name] = value.Value
end
endlocal success local attempt = 0 repeat success = pcall(function() DataSave:UpdateAsync(plr.UserId, function(oldData) oldData = oldData or {} for name, value in pairs(dataToSave) do oldData[name] = value end return oldData end) end) if not success then warn("Failed to save data, retrying...") wait(1) end attempt += 1 until success or attempt >= 5 wait(delayTime) issave = false end
end
for _, value in pairs(stats:GetChildren()) do
if value:IsA(“IntValue”) then
value.Changed:Connect(function()
task.defer(saveData)
end)
end
end
end)game.Players.PlayerRemoving:Connect(function(plr)
local stats = plr:FindFirstChild(“Data”)
if stats then
local dataToSave = {}
for _, value in pairs(stats:GetChildren()) do
if value:IsA(“IntValue”) then
dataToSave[value.Name] = value.Value
end
endlocal success local attempt = 0 repeat success = pcall(function() DataSave:UpdateAsync(plr.UserId, function(oldData) oldData = oldData or {} for name, value in pairs(dataToSave) do oldData[name] = value end return oldData end) end) if not success then warn("Failed to save data, retrying...") wait(1) end attempt += 1 until success or attempt >= 5
end
end)function ServerShutdown()
if RunService:IsStudio() then return end
print(“ServerShutdown!”)
for _, player in pairs(game.Players:GetPlayers()) do
task.spawn(function()
local stats = player:FindFirstChild(“Data”)
if stats then
local dataToSave = {}
for _, value in pairs(stats:GetChildren()) do
if value:IsA(“IntValue”) then
dataToSave[value.Name] = value.Value
end
endlocal success local attempt = 0 repeat success = pcall(function() DataSave:UpdateAsync(player.UserId, function(oldData) oldData = oldData or {} for name, value in pairs(dataToSave) do oldData[name] = value end return oldData end) end) if not success then warn("Failed to save data, retrying...") wait(1) end attempt += 1 until success or attempt >= 5 end end)
end
task.wait(5)
endgame:BindToClose(ServerShutdown)