attempt to call field 'Send' (a nil value)

Hello.

I am trying, somewhat successfully, to make a gamemode. Currently I’m trying to make it so that when you click a button in a window it gives you a gun.

I have all the window stuff sorted out, but to give the player a gun, I need to send the command from the server to the client. I’ve read about how all this works, and as far as I can tell I’ve done nothing wrong.
I have added the string to the network, and on the server end I have this:



concommand.Add("select_m1a1", 
	function(ply,cmd)
	local table = {ply}
		net.Start("select_m1a1",false)
		print("It worked")
		net.Send(ply:Iceotty)
end
)


on the client end I have:



net.Receive("select_m1a1",function select_m1a1(ply)
	ply:Give("m9k_mp5sd")
end)
concommand.Add("select_m1a1", select_m1a1)


I probably don’t need the concommand.Add bit. Also I am aware that the gun I’m trying to give to the player isn’t an m1a1. I picked one at random just to see if it would work.

As far as I can tell, I’m not doing anything wrong. However, when I put “select_m1a1” into the console, I get an error message “attempt to call field ‘Send’ (a nil value)”. It also doesn’t print out that line I put in.

NEXT PROBLEM:

I’m also having trouble with calling the functions I’m using to change class. I have this function:



	function select_sniper( ply )
	ply:Spawn()
	player_manager.SetPlayerClass(ply,"player_sniper")
	print("sniper selected")
end 
concommand.Add( "select_sniper", select_sniper)

It prints the text, but doesn’t do any of the other things I’m telling it to do. This is serverside btw.
I don’t get any errors with this, it just doesn’t do anything.

Any help would be greatly appreciated!

why not just do, however you said on the client you are using the function Give() which you can only use server side.

[lua]
concommand.Add(“select_m1a1”, function(ply,cmd)
net.Start(“select_m1a1”,false)
net.Send(ply)
end)
[/lua]

net.Send it’s serversided guys…

concommands are already networked internally. No need to go again.

I was doing that, but I separated it out more so it was easier to read. Also I tried using Give() serverside, which would be a lot easier, but it doesnt work. Unfortunately the gmod wiki doesnt tell you whether functions are clientside, serverside or both, so I have to go by trial and error. You could be right, because I can’t call the clientside function without net.Send(), but I was getting errors telling me it was null, which I took to mean it was a clientside function.

They do. Blue is serverside, orange is clientside, green is menu-state.

I’m not that experienced with coding in gmod so take note from the others guys, I just tried suggesting what I know.

Aaah ok thanks. Can you tell me why it was saying it was null then?

If it said NULL and not nil, then you were using an entity incorrectly.

Sorry, used to using java :stuck_out_tongue:
Pretty sure it was nil, but I’ll have to check tomorrow morning

Just tried it, it’s saying nil.
The error, copy-pasted is:

and my code is



function select_m1a1(ply)
	ply:Give("m9k_mp5sd")
end

concommand.Add( "select_m1a1", select_m1a1)


I haven’t got the function inside the concommand.Add because it kept giving me strange errors telling me I had an extra bracket when I didn’t.
Sorry I took a while :confused:

I can’t find anything wrong with your code, you could try throwing some prints in there to see what’s going on.



function select_m1a1(ply)
        print("Giveing to",ply)
	ply:Give("m9k_mp5sd")
end

concommand.Add( "select_m1a1", select_m1a1)


To be sure you’re using concommand.Add() correctly (since you said it was giving you errors), this is the correct usage:



concommand.Add("foo",function(ply,cmd,args)
        print("Player ", ply, type(ply)," used concommand ", cmd,type(cmd)," with these arguments:")
        PrintTable(args) --args is a table with only integer indexes, most lua tables like this start from index 1 instead of 0
end)-- <-Don't forget this close parentheses! It closes the concommand.Add() function


player.Give is only server side, guessing from the error paths you have this running on both the server and client state.

Ohh, is it because i’ve got it in shared.lua on my server?

No, that wouldn’t make sense. It would make sense if :Give was setting a var and then you tried to call said var on the client but ply gotten on the client should work on both.

[editline]6th December 2016[/editline]

Lol, where you are defining your ply for your function arguement?

It does make sense, and what are you on about setting variables? If the method is server side only, which it is (

Player:Give), then it won’t exist on the client and as such will error because you tried to call a nil value.

Do you not see him define it in the function?

[editline]6th December 2016[/editline]

This depends on the include setup of your files, I can’t say for sure without seeing that but for a guess that’s probably why.

Kek, I never used concommand before. I thought ply wasn’t being defined. Although if he defines ply in a shared hook, like concommand, it should work as a ply value for a server function.

I don’t know anything about how the files work tbh.

If I don’t want it to be in shared.lua, where should I put it instead? I tried putting it in a different file and I got the same error message.

as for my include setup, I have this in init.lua (serverside)



AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile("player_class/player_assault.lua")
AddCSLuaFile("player_class/player_heavy.lua")
AddCSLuaFile("player_class/player_sniper.lua")
util.AddNetworkString( "set_class" )
util.AddNetworkString( "set_team" )
util.AddNetworkString( "select_m1a1" )
include( "shared.lua" )


[editline]7th December 2016[/editline]

Actually, just tried putting it in init.lua. Doesnt give me an error anymore, but it still doesnt actually do anything. Won’t even print anything.

[editline]7th December 2016[/editline]

I tell a lie, it’s printing to the server console. Guess it makes sense given I’m calling print on the server. It’s also telling me it doesn’t have the weapon I’m giving it. The error message is:

m9k_barret_m82 is a mod weapon, but the server has the mod, so that shouldn’t be a problem. I also tried gmod_tool instead and it still didn’t work.

You must be using the wrong class name then. Try with weapon_crowbar. Also, don’t network it.

For server and client files you would have to either put them in client and server folders or have an if SERVER/CLIENT check.