Attempt to call a nil value from a ModuleScript function

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

Oops, forgot to include the LocalScript.

game.ReplicatedStorage.PingRemoteEvent.OnClientEvent:Connect(function(player) 	

game.ReplicatedStorage.PingRemoteEvent:FireServer()
 
end)

Is that error coming from the ModuleScript or the script? I’m confused.

Normal script. Module returns zero errors. The spawn function does not run for some reason. All of the prints at crucial points to have the module run smoothly did not print for some reason.

Your indentation does not match your function scopes, so you should go over them. You’re trying to call a function which is not defined until you call checkLagger, which might be not your intended behaviour.

1 Like

Yeah, the indentation in the script is a bit messy. But what am I trying to change here to ensure that it isn’t calling a function that isn’t even defined yet?

Define your functions in global scope and not inside another function definition. Proper indentation would make this very easy to see.

3 Likes