502: API Services rejected request with error. HTTP 403 (Forbidden)

Hi I was making a game where to player collects coins and it would save to DataStore. Whenever I play it says 502: API Services rejected request with error. HTTP 403 (Forbidden) On other posts I’ve seen people say that it was because they weren’t the owner of the place, however I am the owner.

Source Of This Problem

local DSS = game:GetService(“DataStoreService”)
local Money = DSS:GetDataStore(“Coins”)

game.Players.PlayerAdded:Connect(function(plr)
local pCoins = Money:GetAsync(plr.UserId)or 0
local leaderstats = Instance.new(“Folder”,plr)
leaderstats.Name = “leaderstats”
local CoinValue = Instance.new(“NumberValue”)
CoinValue.Name = “Coins”
CoinValue.Value = pCoins

-- Set up table to return to any script that requires this module script

local PlayerStatManager = {}

local DataStoreService = game:GetService(“DataStoreService”)
local playerData = DataStoreService:GetDataStore(“PlayerData”)

– Table to hold player information for the current session
local sessionData = {}

local AUTOSAVE_INTERVAL = 10

CoinValue.Parent = leaderstats
CoinValue.Changed:connect(function(v)
	Money:SetAsync(plr.UserId,v)
	print("Money Saved")
end)

end)

Thank you for your time.

1 Like

I can’t really tell you much but 3 things

  • Try out this piece of code when using GetAsync() instead;
local pCoins
if Money:GetAsync(plr.UserId) ~= nil and Money:GetAsync(plr.UserId) ~= "" then
    pCoins = Money:GetAsync(plr.UserId)
else
    pCoins = 0
end
  • This will check to see if the player actually has data instead of just setting it to 0
  • You should add some print() in between certain sections of the code like before the SetAsync() and GetAsync() to see where the error is occuring.
  • If you’re testing in studio, make sure ApiServices is enabled (I doubt this is the cause but you never know)
3 Likes

So I substitute this local pCoins = Money:GetAsync(plr.UserId)or 0 with the code you put? This code was working a few days ago but now it’s not.

Oh it was working a few days ago? Datastore probably broke, but I would replace the code just to be sure. (I don’t know much on datastore breaks because I’ve never had that issue before so I can’t say for sure if it broke.)

I added it in like this, but it still didn’t work. It said 502: API etc. and when I clicked on it, it brought me to line six. I also lost my progress of this past weeks work. Then I redid everything. Could that have been the reason why this isn’t working, and could a virus from free models cause my work to be deleted?

local DSS = game:GetService(“DataStoreService”)
local Money = DSS:GetDataStore(“Coins”)

game.Players.PlayerAdded:Connect(function(plr)
local pCoins
if Money:GetAsync(plr.UserId) ~=nil and Money:GetAsync(plr.UserId) ~= “” then
pCoins = Money:GetAsync(plr.UserId)
else
pCoins = 0
end

local leaderstats = Instance.new(“Folder”,plr)
leaderstats.Name = “leaderstats”
local CoinValue = Instance.new(“NumberValue”)
CoinValue.Name = “Coins”
CoinValue.Value = pCoins

-- Set up table to return to any script that requires this module script

local PlayerStatManager = {}

local DataStoreService = game:GetService(“DataStoreService”)
local playerData = DataStoreService:GetDataStore(“PlayerData”)

– Table to hold player information for the current session
local sessionData = {}

local AUTOSAVE_INTERVAL = 10

CoinValue.Parent = leaderstats
CoinValue.Changed:connect(function(v)
	Money:SetAsync(plr.UserId,v)
	print("Money Saved")
end)

end)

Hmm line 6 is the GetAsync() line. Something must be wrong there.

Okay, I have copied and pasted the script exactly without;

 -- Set up table to return to any script that requires this module script
local PlayerStatManager = {}

local DataStoreService = game:GetService(“DataStoreService”)
local playerData = DataStoreService:GetDataStore(“PlayerData”)

-- Table to hold player information for the current session
local sessionData = {}

local AUTOSAVE_INTERVAL = 10

and it worked completely fine. I believe it is your place that may be the problem. Again check to see if studio has API Services enabled or try the script in a different place to confirm that the place that this script is in currently is the problem.

  • Exact script I used;
local DSS = game:GetService("DataStoreService")

local Money = DSS:GetDataStore("Coins")

game.Players.PlayerAdded:Connect(function(plr)

local pCoins

if Money:GetAsync(plr.UserId) ~=nil and Money:GetAsync(plr.UserId) ~= "" then

pCoins = Money:GetAsync(plr.UserId)

else

pCoins = 0

end

local leaderstats = Instance.new("Folder",plr)

leaderstats.Name = "leaderstats"

local CoinValue = Instance.new("NumberValue")

CoinValue.Name = "Coins"

CoinValue.Value = pCoins

CoinValue.Parent = leaderstats

CoinValue.Changed:connect(function(v)

Money:SetAsync(plr.UserId,v)

print("Money Saved")

end)

end)

Once I’m having the same problem, I’m using this topic.

I created a fresh new project and I only put this code inside ServerScriptService:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerUserId

local sessionData = {}
local playerData = DataStoreService:GetDataStore("PlayerData", playerUserId)
 
Players.PlayerAdded:Connect(function(player)
	playerUserId = "Player_" .. player.UserId
	local data = playerData:GetAsync(playerUserId)
	print('------- PlayerAdded --------')
	print(data)
end)
 
Players.PlayerRemoving:Connect(function(player)
	local data = {a=1, b=2}
	print('------- PlayerRemoving --------')
	print(data)
	playerData:SetAsync(playerUserId, data)
end)

When I run, I got this:

  502: API Services rejected request with error. HTTP 403 (Forbidden)
Stack Begin
Script 'ServerScriptService.Script', Line 11
Stack End

(Obs: Line 11 = local data = playerData:GetAsync(playerUserId))

Here my current game options:

Can’t save the data…

What am I missing?

Isn’t this error just telling you to turn on the service enabling DataStores from game settings?

Perhaps you added something to your code recently that can’t be saved. Like an object.

Ah I think I know the problem

local playerUserId

local sessionData = {}
local playerData = DataStoreService:GetDataStore("PlayerData", playerUserId)

playerUserId is nil, and when you’re getting data you should only provide a string from what I recall.

Okay I fixed your issue, the main problem is because you were testing solo mode in studio though.

local Players = game:GetService("Players")
local datastoreservice = game:GetService("DataStoreService"):GetDataStore("TEST")
 
Players.PlayerAdded:Connect(function(player)
	local key = player.userId
	local playerData = datastoreservice:GetAsync(key)
	if playerData then
		print(playerData.a, playerData.b)
	else
		print'no data'
	end
end)
 
Players.PlayerRemoving:Connect(function(player)
	local data = {
	}
	data.a = 1
	data.b = 2
	local key = player.userId
	datastoreservice:SetAsync(key, data)
end)

Ok, this code doesn’t shows the error, but at same time it doesn’t save anything when the user exits… Always shows ‘no data’, SetAsync not saving… (crazy Roblox Server behaviors)…

In time: today I tested my original code again and TODAY IT’S NOT SHOWING THE ERROR. And not saving anything though…