I started using HTTP service in my game today to replicate a table across multiple places. However, sometimes the studio/game takes too long or doesn’t work and the script gets completely broken. Any ideas on how to solve this issue?
(The Pastebin has 552 lines of dictionaries and lists)
local httpService = game:GetService("HttpService")
local url = "https://pastebin.com/raw"
local leaguesTable
pcall(function()
leaguesTable = httpService:JSONDecode(httpService:GetAsync(url))
end)
if not leaguesTable then
warn("Data wasn't found!")
end
HTTP Requests can be kind of “on and off”. A good practice is to wrap them in a pcall. I’d recommend that if that’s a static table, just store it in a module script. If it is being constantly updated, I’d just store it in the game’s datastore, and retrieve it at place start. You can either save the table to the datastore using a script, or using a plugin like Datastore Editor.
I used to have ModuleScripts doing the work but my game divided into 5 places and it was painful to open each one daily just to change 1/2 items. About the game’s datastore I’m going to look more into it.
It could also be pastebin. I’ve never personally worked with it, but the API could just be slow. You could try trying to using an HTTP get on some other site. Also, this might be dumb of me to ask, but you have Allow HTTP Requests enabled in the game settings, right?
Yes, allow HTTP requests is turned on with sometimes the script working the way it should be. As for Pastebin being slow, do you have any other API recomendations?
I haven’t really used HTTPService before, so I don’t really have a “favorite” API. I would imagine that storing everything in a github repo like wally does would probably be pretty consistent. I also know that a lot of people use Trello for stuff. The most prominent example of that is probably in conjunction with Adonis Admin to handle and persist bans. Both of those sites have their own API documentation you could find easily.
sometimes the script working the way it should be
if the script works correctly–even if only sometimes–then it’s likely not the fault of an underlying issue with any of your code on HTTPService. It seems like it’s just either hang-ups on the API’s end, or on Roblox’s end. I would recommend wrapping the HTTP in a pcall to catch errors, and/or using a library like Promise (by evaera or Quenty; they’re both good in their own way) to handle your HTTP requests. (If it’s a problem with the API, then Promises won’t help, but if it’s like @Dede_4242 said, that you just need to wait longer, promises have a great API for handling values that don’t exist, but will).