When my script attempts to call the ping from the module, it calls a nil value and returns an error, which results in NetworkOwnership not switching to the server or to the player. Does anyone know why? Here are the ModuleScript and the script calling the function respectively below.
local pings = {}
game.ReplicatedStorage.PingRemoteEvent.OnServerEvent:Connect(function(player)
local pingInfo = pings[player]
if pingInfo and pingInfo.sent then
pingInfo.received = tick()
pingInfo.ping = math.clamp(pingInfo.received - pingInfo.sent, 0, 5)
pingInfo.sent = nil
end
end)
local function playerAdded(player)
pings[player] = {
ping = 1
}
end
game.Players.PlayerAdded:Connect(playerAdded)
for _, player in next, game.Players:GetPlayers() do
playerAdded(player)
end
game.Players.PlayerRemoving:Connect(function(player)
pings[player] = nil
end)
function NearestBall(position)
local list = game.Workspace:GetChildren()
local ball
local dist = 31
local temp, temp2
for x = 1, #list do
temp2 = list[x]
if temp2.Name == "Ball" then
temp = temp2
if dist > (temp.Position - position.Position).Magnitude then
ball = temp
dist = (temp.Position - position.Position).Magnitude
end
end
end
return ball
end
function checkLagger(player, pingInfo)
local Character = player.Character or player.CharacterAdded:Wait()
if pingInfo.sent ~= nil then
local ball = NearestBall(Character.LowerTorso.Position)
if ball then
if ball.NetworkOwner.Value == player then
ball.NetworkOwner.Value = nil
ball:SetNetworkOwner(nil)
end
end
spawn(function()
while true do
for player, pingInfo in next, pings do
coroutine.resume(coroutine.create(function()
pingInfo.sent = tick()
game.ReplicatedStorage.PingRemoteEvent:FireClient(player)
wait(0.4)
checkLagger(player, pingInfo)
end))
end
wait(1)
end
end)
function pings:getPing(player)
return pings[player] and pings[player].ping or 1
end
end
end
return pings
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("GameRemotes")
local status = Remotes:WaitForChild("ValueChange")
local pingFramework = require(script.PingFramework)
status.OnServerEvent:Connect(function(player, hit, value)
if hit.Anchored == false and hit.Parent == workspace then
local magnitude = (hit.Position - player.Character.LowerTorso.Position).magnitude
if magnitude > 15 then return end
local ping = math.floor(pingFramework:getPing(player) * 1000 + 0.5)
if ping > 500 then
if hit.NetworkOwner.Value == player then
hit.NetworkOwner.Value = nil
hit:SetNetworkOwner(nil)
return
end
end