This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PingTimes.lua
79 lines (55 loc) · 1.81 KB
/
PingTimes.lua
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
-- Require this module from the client and the server
-- Server can then do PingTimes[Player] to get a player's ping
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Postie = require(script:WaitForChild("Postie"))
if RunService:IsClient() then
-- Handle client end of ping logic
Postie.SetCallback("RequestPing", function(GUID)
-- We force the player to return a GUID to validate the ping, since
-- any exploiter cannot have known the GUID in advance to fake it
return GUID
end)
return nil
else
-- Handle server side of ping logic, and expose data
local UPDATE_FREQUENCY = 3
local Http = game:GetService("HttpService")
local PingTimes = {}
local function PingFunc(Player)
local GUID = Http:GenerateGUID(false)
local StartClock = os.clock()
local isSuccessful, returnedGUID = Postie.InvokeClient("RequestPing", Player, 3, GUID)
if isSuccessful and (GUID == returnedGUID) then -- Validate
PingTimes[Player] = os.clock()-StartClock
end
end
local PingingThread = coroutine.create(function()
while wait(UPDATE_FREQUENCY) do
for _, Player in pairs(Players:GetPlayers()) do
local PlayerThread = coroutine.create(PingFunc)
coroutine.resume(PlayerThread, Player)
end
end
end)
coroutine.resume(PingingThread)
Players.PlayerRemoving:Connect(function(Player)
PingTimes[Player] = nil
end)
Players.PlayerAdded:Connect(function(Player)
wait(1) -- let player load in and get stable connectivity
PingFunc(Player)
end)
return setmetatable({}, {
__index = function(tab, key)
if typeof(key) == "Instance" and key:IsA("Player") then
return PingTimes[key] or 0.075
else
return nil
end
end;
__newindex = function(tab, key,value)
warn("Cannot set PingTime manually")
end;
})
end