DataStore is loss

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
end

local dataValues = {
{“Cash”, 0}, {“Gem”, 0},
}

for _, data in pairs(dataValues) do
createIntValue(data[1], data[2], stats)
end

local 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
end

loadData()

local issave = false
local delayTime = 5

local 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
end

  	local 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
end

  local 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
end

  		local 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)
end

game:BindToClose(ServerShutdown)

1 Like

You probably don’t have “Studio API Services” enabled.

To fix this, go into your game in Roblox Studio. Click “File” in the top left. Go to “Game Settings”. Go to the “Security” tab and turn on “Enable Studio Access to API Services”.

The place also has to saved to Roblox for this to work.

1 Like

​​​​​​​​​​​
I’m already Enabled

Is this coming from a LocalScript or server Script? That’s the only other thing I can think of.

1 Like

it is from the ServerScript do you know how to fix it?

If you have API Services enabled and everything’s coming from a server Script then it shouldn’t really come up with this error, so no, unfortunately.

Is it because I tested it in Roblox Studio?