-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserverhop.lua
More file actions
113 lines (89 loc) · 2.88 KB
/
serverhop.lua
File metadata and controls
113 lines (89 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
return function(placeid, smallServer)
local TeleportService = game:GetService("TeleportService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local CACHE_TIME = 300
local folder = "servers"
local filename = folder .. "/" .. placeid .. (smallServer and "L" or "H") .. ".json"
if not isfolder(folder) then
makefolder(folder)
end
local function now()
return os.time()
end
local function LoadCache()
if not isfile(filename) then return nil end
local ok, data = pcall(function()
return HttpService:JSONDecode(readfile(filename))
end)
if not ok or not data.timestamp or not data.data then
return nil
end
if now() - data.timestamp > CACHE_TIME then
return nil
end
return data.data
end
local function SaveCache(data)
writefile(filename, HttpService:JSONEncode({
timestamp = now(),
data = data
}))
end
local function FetchServers(cursor)
local url =
"https://games.roblox.com/v1/games/" .. placeid ..
"/servers/Public?limit=100&excludeFullGames=true&sortOrder=" .. (smallServer and "Asc" or "Desc") ..
(cursor and "&cursor=" .. cursor or "")
local raw = game:HttpGet(url)
return HttpService:JSONDecode(raw)
end
local Servers = LoadCache()
if not Servers then
local collected = { data = {}, nextPageCursor = nil }
local cursor = nil
repeat
local page = FetchServers(cursor)
if page and page.data then
for _, s in ipairs(page.data) do
table.insert(collected.data, s)
end
end
cursor = page.nextPageCursor
until not cursor
Servers = collected
SaveCache(Servers)
end
local function RemoveServer(serverId)
if not isfile(filename) then return end
local cache = HttpService:JSONDecode(readfile(filename))
local new = {}
for _, s in ipairs(cache.data.data) do
if s.id ~= serverId then
table.insert(new, s)
end
end
cache.data.data = new
writefile(filename, HttpService:JSONEncode(cache))
end
local target = nil
for _, s in ipairs(Servers.data) do
if s.playing < s.maxPlayers and s.id ~= game.JobId then
target = s
break
end
end
if not target then
return warn("No valid server found")
end
RemoveServer(target.id)
TeleportService:TeleportToPlaceInstance(
placeid,
target.id,
Players.LocalPlayer
)
task.spawn(function()
task.wait(10)
TeleportService:TeleportToPlaceInstance(placeid, game.JobId, Players.LocalPlayer)
end)
end