Indexing a nil value despite value not being nil? Also, networking problem.

Hello. This is a strange error I’m getting, it throws this error in the console on startup:



[ERROR] addons/pizzadelivery/lua/delivery/sv_hooks.lua:8: attempt to index local 'ply' (a nil value)
  1. v - addons/pizzadelivery/lua/delivery/sv_hooks.lua:8
   2. unknown - lua/includes/modules/hook.lua:84

in the code:


hook.Add("Think", "CheckMission", function()
	if DELIVERY:CheckIfActiveMissions() then
		local missions = DELIVERY.ACTIVEMISSIONS
		for k,v in pairs(missions) do
			local ply = DELIVERY:FindPlayerWithName(k)
			local area = DELIVERY.MISSIONAREAS[v]
			
			local plyPos = ply:GetPos()
			if plyPos:WithinAABox(area[1][1], area[1][2]) then
				print("IN BOX")
				DELIVERY:FinishMission(ply)
			end
		end
	end
end)


When I check the value ply w/ print(ply), it shows the correct name, so it can’t be nil?
I’m not sure if it’s a problem with the way I’m including my different files, but it doesn’t seem like it, since the functions are included before the hooks. (Not sure exactly how that works.)

Also, I have another problem, with networking.

I include my files as such:


include("delivery/sv_info.lua")
include("delivery/sv_networking.lua")
include("delivery/sv_config.lua")
include("delivery/sv_functions.lua")
include("delivery/sv_hooks.lua")

AddCSLuaFile("delivery/cl_networking.lua")

with the important files

cl_networking.lua:


print("PizzaDelivery Client Mounted OK")

net.Receive( "PizzaDelivered", function()
print("recieved")
local amnt = net.ReadFloat()
notification.AddLegacy("You have delievered a pizza and received "..amnt.."dollars.", NOTIFY_GENERIC, 3)
end)

and sv_networking.lua:


util.AddNetworkString("PizzaDelivered")

And I send a network event in my sv_functions.lua:


function DELIVERY:FinishMission(plyDeliver)
	print("isrunning")
	local missions = DELIVERY.ACTIVEMISSIONS
	for k,v in pairs(missions) do
		if k == string.lower(plyDeliver:GetName()) then
			local PizzaMoney = DELIVERY.REWARD or 100
			missions[k] = nil
			plyDeliver:addMoney(PizzaMoney)
			net.Start("PizzaDelivered")
			net.WriteFloat(PizzaMoney)
			print("deliverednetwork")
			net.Send(plyDeliver)
		end
	end
	
end

With all of the debug prints working, but it doesn’t show up on the client. Are the inclusions wrong, or is it something else? (Very new to networking and gmod file structure, maybe I’ve structured this addon wrong or something?)

Any help is appreciated, thanks.

It’s pretty obvious that “DELIVERY:FindPlayerWithName(k)” returns nil.

Nope. When I do print(DELIVERY:FindPlayerWithName(k)) in that hook it outputs


Player [1][overki11]

Just tried it, infact.

It only throws this error in the startup, for some reason. During gameplay the hook seems to execute, and everything works. Is the error maybe part of of my inclusions?

Most likely it runs the script WHILST it boots, where obviously no player is online, add a check if a player exists, else stop the hook.

Yeah, I came to that realization, because I did have a debug mission consisting of myself.

I removed that, and then tried, and I got an error about DELIVERY:CheckIfActiveMissions(), but then I reloaded the game and it seemed to work… strange. Anyway, that’s fixed, but I still have the networking problem. I’m not sure I put the net.Receive in the right place…

try reading the float before printing “received”. Dunno if that net rule applies even there.

Nope. No output from the float, or the received print.

Add a print after



if k == string.lower(plyDeliver:GetName()) then


And see if its actually ran, if not, then you know what the problem is.

I have. It did run.

Huh, either I am stupid or I dont know further, sorry.