forked from P3nguinMinecraft/MiscScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverhop.lua
More file actions
72 lines (58 loc) · 2.13 KB
/
serverhop.lua
File metadata and controls
72 lines (58 loc) · 2.13 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
return function(placeId)
local TeleportService = cloneref(game:GetService("TeleportService"))
local HttpService = cloneref(game:GetService("HttpService"))
local Players = cloneref(game:GetService("Players"))
TeleportService:TeleportCancel()
local function listServers(cursor)
local url = (
"https://games.roblox.com/v1/games/%s/servers/Public?sortOrder=Asc&limit=100&excludeFullGames=true"
):format(placeId)
if cursor then
url = url .. "&cursor=" .. cursor
end
local raw
pcall(function()
raw = game:HttpGet(url)
end)
if not raw then return nil end
local decoded = HttpService:JSONDecode(raw)
if decoded.errors then return nil end
return decoded
end
local nextCursor = nil
local chosenServer = nil
while not chosenServer do
local servers = listServers(nextCursor)
if not servers or not servers.data then
warn("Servers API cooldown or invalid response.")
task.wait(2)
else
local pool = {}
for i = 1, #servers.data do pool[i] = i end
while #pool > 0 do
local randIndex = math.random(#pool)
local index = table.remove(pool, randIndex)
local s = servers.data[index]
if s and s.playing < s.maxPlayers and s.id ~= game.JobId then
chosenServer = s
break
end
end
if not chosenServer then
if servers.nextPageCursor then
nextCursor = servers.nextPageCursor
else
warn("No available servers found.")
return
end
end
end
end
print("Teleporting to:", chosenServer.id)
TeleportService:TeleportToPlaceInstance(placeId, chosenServer.id, Players.LocalPlayer)
task.spawn(function()
task.wait(10)
warn("Teleport failed, retrying...")
TeleportService:TeleportToPlaceInstance(placeId, chosenServer.id, Players.LocalPlayer)
end)
end