custom function returning nil?

I have this bit here running serverside.


local DoorOwner = "Unowned"

function GetDoorOwner()

	return DoorOwner

end

util.AddNetworkString( "sendOwnerToClient" )
net.Start( "sendOwnerToClient" )
net.WriteString( GetDoorOwner() )
net.Send()


I’m trying to send the door owner’s name to the client. I added a line to print DoorOwner in the chat serverside, but when I net.Recieve sendOwnerToClient, it just returns nil.


function drawDoorInfo()

	for k, v in pairs( ents.GetAll() ) do

		if v:GetClass() == "prop_door_rotating" then

			net.Receive( "sendOwnerToClient", function()

				local OwnerName = net.ReadString()

			end )
                             //cam.Start3D2D goes here
			end

		end

	end

end
hook.Add( "PostDrawOpaqueRenderables", "drawDoorInfo", drawDoorInfo )

Hopefully i made my self completely clear and there is no confussion as to what im attempting to do here.

You didn’t specify who you where sending it to.

net.Send requires you to specify the player which is good if you are only sending it to a specific player or players. However if you want it to be sent to all clients then it would be best to use

net.Broadcast


util.AddNetworkString( "sendOwnerToClient" )
net.Start( "sendOwnerToClient" )
net.WriteString( "test" )
net.Broadcast()

I did this and it still returns nil.
Could it be that the client is trying to receive the data every frame or tick, but the server only sends it once?
Or does the server send it as the net.Receive requests it?

[del]Why did you create a function called get door owner? It already exists within darkRP. http://wiki.darkrp.com/index.php/Functions/Entity/Shared/getDoorOwner[/del]

I don’t think your function is returning anything hence why it is not being sent to the client. You could easily check by adding a print for the door owner.

[editline]24th January 2016 at 07:41PM[/editline]

When getting nil errors the very first step in debugging should be to go back and print the data to see where it becomes nil.

Im not editing darkrp, I’m making my own gamemode. Also, the data becomes nil after i send it to the client.
It returns “unowned” as expected if i get it and print it serverside.
Im not really sure where to go from here. :confused:

That net.Receive is inside a function, which is only ran inside a hook.

Okay, i found something really weird. If i do this outside of drawDoorInfo, it still shows “nil” on the door. It’s not taking the var from the net.Receive.

If I set the var outside of both net.Receive and drawDoorInfo() it works correctly.

EDIT:
It seems that i can’t use data received in net.Receive in drawDoorInfo() because it just returns nil.

That’s because local only applies to the function its created in. If it’s created outside of a function, then it applies to the file.

how would i re-write this so it sets the var outside of it?


net.Receive( "SendOwnerToClient", function()

	local DoorOwnerName = net.ReadString()

end )

That makes the net.Receive pointless. It doesn’t solve the problem. DoorOwnerName would just equal “unowned”…

EDIT:
Reloaded the server and it seems this works.