Hi everyone! This is my first post on the DevForum.
So, you have the rare problem that, you are using HTTPService in your game, but you have a limited request amount and when every Server requests for themselves its gonna reach the limit soon, want one server to request the data and distribute it to everyone else? Well lucky for you i did this already, you can download the code here.
local UpdatePrices = {}
local HTTPService = game:GetService("HttpService")
function UpdatePrices.tick()
local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local joinTopic = "JoinTopic"
local responseTopic = "ResponseTopic"
local removeTopic = "RemoveTopic"
local addTopic = "AddTopic"
local informationTopic = "InformationTopic"
local recieveInformationTopic = "RecieveInformationTopic"
local refreshTopic = "RefreshTopic"
local serverList = {}
local isRootServer = false
-- Example of Data
local recentData = {
gold = 2200,
silver = 36,
platinum = 1080,
palladium = 980,
copper = 0.03,
zinc = 0,
lead = 0,
aluminium = 0,
nickel = 0
}
local exampleFormatServerTable = {
{jobId = 132324, Row = 1},
{jobId = 125432, Row = 2},
{jobId = 532463, Row = 3},
{jobId = 635435, Row = 4}
}
local function getCurrentRowByJobId(jobId)
for _, server in ipairs(serverList) do
if server.jobId == jobId then
return server.Row
end
end
return nil
end
local function respondCurrentData()
MessagingService:SubscribeAsync(informationTopic, function(message)
MessagingService:PublishAsync(recieveInformationTopic, recentData)
print("gave recent Data to other")
end)
end
local function getCurrentData()
MessagingService:SubscribeAsync(recieveInformationTopic, function(message)
print("Got response, got recentData from Other Servers")
recentData = message.Data
end)
MessagingService:PublishAsync(informationTopic, "reqData")
end
local function collectData()
MessagingService:SubscribeAsync(refreshTopic, function(message)
print("Recieved New Data from Root")
recentData = message.Data
end)
end
local function printTable(t)
for key, value in pairs(t) do
print(key, value)
end
end
local function printTableSpecial(t)
for i, innerTable in ipairs(t) do
print("Row " .. i .. ":")
for key, value in pairs(innerTable) do
print(" " .. key .. ": " .. value)
end
end
end
local url = "Your_API_URL"
local function requestData()
local success, response = pcall(function()
return HTTPService:GetAsync(url)
end)
if success then
local jsonResponse = HTTPService:JSONDecode(response)
if jsonResponse then
recentData = {
-- Override your old Data
}
print("Prices updated successfully.")
else
warn("Unexpected JSON response structure")
end
else
warn("Failed to fetch prices: " .. response)
end
end
-- Example of a DataRefreshing Function
local function getData()
if getCurrentRowByJobId(game.JobId) == 1 then
print("Updated Data first Index")
requestData()
printTable(recentData)
end
while wait(14400) do
print("Rstep")
printTableSpecial(serverList)
printTable(recentData)
if getCurrentRowByJobId(game.JobId) == 1 then
isRootServer = true
requestData()
print("Root-Server: Data was refreshed.")
MessagingService:PublishAsync(refreshTopic, recentData)
end
--updateData()
end
end
local function requestServerTable()
local gotresponse = false
MessagingService:SubscribeAsync(responseTopic, function(message)
local serverTable = message.Data
serverList = serverTable
gotresponse = true
print("Got and used Response")
end)
MessagingService:PublishAsync(joinTopic,"reqTable")
wait(1)
MessagingService:SubscribeAsync(joinTopic, function(message)
MessagingService:PublishAsync(responseTopic, serverList)
end)
if not gotresponse then
print("No response, lone Server, setting as Root.")
isRootServer = true
end
MessagingService:PublishAsync(addTopic, game.JobId)
end
local function findNextAvailableRow(tabler)
local maxRow = 0
for _, server in ipairs(tabler) do
if server.Row > maxRow then
maxRow = server.Row
end
end
return maxRow + 1
end
local function addServer()
MessagingService:SubscribeAsync(addTopic, function(message)
local newRow = findNextAvailableRow(serverList)
table.insert(serverList, {jobId = message.Data, Row = newRow})
print("Sucessfully added Server: "..message.Data.." Row: "..newRow)
end)
end
local function moveServersForward(removedRow)
for _, server in ipairs(serverList) do
if server.Row > removedRow then
server.Row = server.Row - 1
end
end
end
local function removeServer()
MessagingService:SubscribeAsync(removeTopic, function(message)
local serverid = message.Data
for i, server in ipairs(serverList) do
if server.jobId == serverid then
local row = server.Row
table.remove(serverList, i)
moveServersForward(row)
print("Sucessfully removed server: "..serverid.." Row: "..row)
end
end
print("Failed to remove server: "..serverid)
end)
end
local function checkShutdown()
if not game:GetService("RunService"):IsStudio() then
MessagingService:PublishAsync(removeTopic, game.JobId)
end
end
addServer()
removeServer()
requestServerTable()
getCurrentData()
respondCurrentData()
collectData()
getCurrentData()
getData()
game:BindToClose(checkShutdown)
end
coroutine.resume(coroutine.create(UpdatePrices.tick))
return UpdatePrices
This Module is currently being used in one of my new games, which revolves around this Modulescript, check it out if you want: Buy Gold but for its actual value game
Yeah so if you got any Questions on how to use this, then reply!
Have a nice day.