DataStore error: Error: 502: API Services rejected request with error. HTTP 500 (Internal Server Error)

I have put AutomaticRetry on. Not sure if this will help anything but yeah. Any help will be appreciated. This is because it is ending up in data loss.

2 Likes

Can I see your script please? This will help me see errors you have.

1 Like

I believe it is an error due to DataStores being down. But would automatic retry help with the data loss or would I need to do something else?

local DataStoreService = game:GetService("DataStoreService")
local MarketplaceService = game:GetService("MarketplaceService")
local HttpService = game:GetService("HttpService")
local LevelStore = DataStoreService:GetOrderedDataStore("minuteDataStore")
local Players = game:GetService("Players")
local Webhook = ""
local PlayersData = Instance.new("Folder", game:GetService("ServerStorage"))
PlayersData.Name = "PlayersData"
Players.PlayerAdded:Connect(function(Player)
	local PlayerData = Instance.new("Folder", PlayersData)
	PlayerData.Name = Player.Name
	local LevelAmount = Instance.new("IntValue", PlayerData)
	LevelAmount.Name = "Levels"
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local MinutesPlayed = Instance.new("IntValue")
	MinutesPlayed.Name = "MinutesPlayed"
	MinutesPlayed.Parent = Leaderstats
	local ILevelAmount = Instance.new("IntValue", PlayerData)
	ILevelAmount.Name = "ILevelAmount"
	ILevelAmount.Value = 1
	local GMS, GMV = pcall(function()
		return LevelStore:GetAsync(Player.UserId)
	end)
	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 9117814) then
		ILevelAmount.Value = ILevelAmount.Value +1
	end
	if GMS then
		LevelAmount.Value = GMV
		MinutesPlayed.Value = LevelAmount.Value
		while true do
			wait(60)
			LevelAmount.Value = LevelAmount.Value+ILevelAmount.Value
			MinutesPlayed.Value = LevelAmount.Value
		end
	else
		local Data = {
			["content"] = ("**Minutes Datastore Error:**\nFailed to retrieve "..Player.Name.."'s minutes. Error: "..GMV..".")
		}
		Data = HttpService:JSONEncode(Data)
		HttpService:PostAsync(Webhook, Data)
	end
end)
Players.PlayerRemoving:Connect(function(Player)
	local LevelAmount = game:GetService("ServerStorage"):WaitForChild("PlayersData"):FindFirstChild(Player.Name).Levels
	local SMS, SMV = pcall(function()
		LevelStore:SetAsync(Player.UserId, LevelAmount.Value)
	end)
	if SMS then
		return
	else
		print(SMV)
		local Data = {
			["content"] = ("**Minutes Datastore Error:**\nFailed to save "..Player.Name.."'s minutes. Error: "..SMV..". **Levels: "..LevelAmount.Value..".**")
		}
		Data = HttpService:JSONEncode(Data)
		HttpService:PostAsync(Webhook, Data)
	end
	wait()
	LevelAmount.Parent:Destroy()
end)
game:BindToClose(function()
	local GetPlayers = Players:GetPlayers()
	for _, Player in pairs(GetPlayers) do
		local LevelAmount = game:GetService("ServerStorage"):WaitForChild("PlayersData"):FindFirstChild(Player.Name).Levels
		local SMS, SMV = pcall(function()
			LevelStore:SetAsync(Player.UserId, LevelAmount.Value)
		end)
		if SMS then
			local Data = {
				["content"] = ("**All players minutes saved successfully during server shutdown.**")
			}
			Data = HttpService:JSONEncode(Data)
			HttpService:PostAsync(Webhook, Data)
		else
			local Data = {
				["content"] = ("**Minutes Datastore Error:**\nFailed to save "..Player.Name.."'s minutes. Error: "..SMV..". **Minutes: "..LevelAmount.Value..".**")
			}
			Data = HttpService:JSONEncode(Data)
			HttpService:PostAsync(Webhook, Data)
		end
	end
end)
game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.sub(Message, 1, 6) == "/medit" then
			if Player.Name == "7h_n" or Player.Name == "DarkEssentials" or Player.Name == "NapCall" then
				local Args = string.split(Message, " ")
				local Success, Result = pcall(function()
					LevelStore:SetAsync(tonumber(Args[2]), tonumber(Args[3]))
				end)
				if Success then
					local TPlayer = Players:GetNameFromUserIdAsync(Args[2])
					if TPlayer then
						local LevelAmount = game:GetService("ServerStorage"):FindFirstChild("PlayersData"):FindFirstChild(TPlayer).Levels
						LevelAmount.Value = tonumber(Args[3])
						Player:WaitForChild("leaderstats"):FindFirstChild("MinutesPlayed").Value = LevelAmount.Value
					end
					local Data = {
						["content"] = ("**"..Player.Name.."("..Player.UserId..")** Has edited: **"..Args[2].."**'s minutes. Updated minutes to: **"..Args[3].."**.")
					}
					Data = HttpService:JSONEncode(Data)
					HttpService:PostAsync(Webhook, Data)
				elseif not Success then
					warn(("Level Refund Error: "..Result.."."))
					local Data = {
						["content"] = ("Minutes Refund Error: "..Result..".")
					}
					Data = HttpService:JSONEncode(Data)
					HttpService:PostAsync(Webhook, Data)
				end
			end
		end
	end)
end)
1 Like
1 Like

I would use a pcall function to prevent this from happening

1 Like

I already am, I posted the code above.

This isn’t related as the code will fail anyways. Pcalling is only used for catching not avoiding errors. It is clear that this is a Roblox issue and bug, a post of which is linked in my previous post.

Cc: @zrax_rb

1 Like

I read the post, I couldn’t see a fix for it in the replies. Is there any way I can do anything, would activating AutomaticRetry help with this?

No because you can get stuck in an infinite loop if the issue is consistent. You can wait until a fix patched for now. :sun_with_face:

2 Likes

How come other games do not experience this data loss then? Are they caching the data?

Perhaps it does not impact all games, but rather certain ones. I suggest liking the bug report to grab further staff attention so it can get fixed.

1 Like

I have disabled AutomaticRetry as you said it would not do anything but get me stuck in an infinite loop, but I can’t just sit around losing players data. I think I could store the data, then retry the save every 10 minutes. But this does not help with retrieving data.