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:
[CODE]
concommand.Add("select_m1a1",
function(ply,cmd)
local table = {ply}
net.Start("select_m1a1",false)
print("It worked")
net.Send(ply:Iceotty)
end
)
[/CODE]
on the client end I have:
[CODE]
net.Receive("select_m1a1",function select_m1a1(ply)
ply:Give("m9k_mp5sd")
end)
concommand.Add("select_m1a1", select_m1a1)
[/CODE]
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:
[CODE]
function select_sniper( ply )
ply:Spawn()
player_manager.SetPlayerClass(ply,"player_sniper")
print("sniper selected")
end
concommand.Add( "select_sniper", select_sniper)[/CODE]
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...
[QUOTE=Benjii;51456370]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][/QUOTE]
concommands are already networked internally. No need to go again.
[QUOTE=Benjii;51456370]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][/QUOTE]
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.
[QUOTE=Iceotty;51456810]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.[/QUOTE]
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.
[QUOTE=code_gs;51456830]They do. Blue is serverside, orange is clientside, green is menu-state.[/QUOTE]
Aaah ok thanks. Can you tell me why it was saying it was null then?
[QUOTE=Iceotty;51457144]Aaah ok thanks. Can you tell me why it was saying it was null then?[/QUOTE]
If it said NULL and not nil, then you were using an entity incorrectly.
[QUOTE=code_gs;51457303]If it said NULL and not nil, then you were using an entity incorrectly.[/QUOTE]
Sorry, used to using java :P
Pretty sure it was nil, but I'll have to check tomorrow morning
[QUOTE=code_gs;51457303]If it said NULL and not nil, then you were using an entity incorrectly.[/QUOTE]
Just tried it, it's saying nil.
The error, copy-pasted is:
[QUOTE]
[ERROR] gamemodes/pvp/gamemode/shared.lua:55: attempt to call method 'Give' (a nil value)
1. unknown - gamemodes/pvp/gamemode/shared.lua:55
2. unknown - lua/includes/modules/concommand.lua:54
[/QUOTE]
and my code is
[CODE]
function select_m1a1(ply)
ply:Give("m9k_mp5sd")
end
concommand.Add( "select_m1a1", select_m1a1)
[/CODE]
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 :/
[QUOTE=Iceotty;51486151]Just tried it, it's saying nil.
The error, copy-pasted is:
and my code is
~code~
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 :/[/QUOTE]
I can't find anything wrong with your code, you could try throwing some prints in there to see what's going on.
[code]
function select_m1a1(ply)
print("Giveing to",ply)
ply:Give("m9k_mp5sd")
end
concommand.Add( "select_m1a1", select_m1a1)
[/code]
To be sure you're using concommand.Add() correctly (since you said it was giving you errors), this is the correct usage:
[code]
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
[/code]
[QUOTE=Iceotty;51486151]Just tried it, it's saying nil.
The error, copy-pasted is:
and my code is
[CODE]
function select_m1a1(ply)
ply:Give("m9k_mp5sd")
end
concommand.Add( "select_m1a1", select_m1a1)
[/CODE]
[/QUOTE]
player.Give is only server side, guessing from the error paths you have this running on both the server and client state.
[QUOTE=bigdogmat;51486643]player.Give is only server side, guessing from the error paths you have this running on both the server and client state.[/QUOTE]
Ohh, is it because i've got it in shared.lua on my server?
[QUOTE=bigdogmat;51486643]player.Give is only server side, guessing from the error paths you have this running on both the server and client state.[/QUOTE]
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]
[QUOTE=Iceotty;51486750]Ohh, is it because i've got it in shared.lua on my server?[/QUOTE]
Lol, where you are defining your ply for your function arguement?
[QUOTE=FlyPiggyBanks;51486754]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.[/quote]
It does make sense, and what are you on about setting variables? If the method is server side only, which it is ([img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/Give]Player:Give[/url]), then it won't exist on the client and as such will error because you tried to call a nil value.
[quote]
Lol, where you are defining your ply for your function arguement?[/QUOTE]
Do you not see him define it in the function?
[editline]6th December 2016[/editline]
[QUOTE=Iceotty;51486750]Ohh, is it because i've got it in shared.lua on my server?[/QUOTE]
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.
[QUOTE=bigdogmat;51486818]It does make sense, and what are you on about setting variables? If the method is server side only, which it is ([img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/Give]Player:Give[/url]), 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.[/QUOTE]
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)
[CODE]
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" )
[/CODE]
[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:
[QUOTE]
Attempted to create unknown entity type m9k_barret_m82!
NULL Ent in GiveNamedItem!
[/QUOTE]
m9k_barret_m82 [I]is[/I] 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.
[QUOTE=Iceotty;51492740]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)
[CODE]
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" )
[/CODE]
[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 [I]is[/I] 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.[/QUOTE]
For server and client files you would have to either put them in client and server folders or have an if SERVER/CLIENT check.
[QUOTE=code_gs;51493523]You must be using the wrong class name then. Try with weapon_crowbar. Also, don't network it.[/QUOTE]
dw, im not networking it. weapon_crowbar works. So apparently the file names of the weapons aren't their class names? Or is it because I just copy pasted the mod files into the addon folder of the server, rather than actually downloading them?
[editline]7th December 2016[/editline]
[QUOTE=FlyPiggyBanks;51493577]For server and client files you would have to either put them in client and server folders or have an if SERVER/CLIENT check.[/QUOTE]
That doesnt help, or make any sense to me.
[QUOTE=Iceotty;51493682]dw, im not networking it. weapon_crowbar works. So apparently the file names of the weapons aren't their class names? Or is it because I just copy pasted the mod files into the addon folder of the server, rather than actually downloading them?
[editline]7th December 2016[/editline]
That doesnt help, or make any sense to me.[/QUOTE]
Either put your server files in addon/lua/autorun/sever/serverfiles.lua
and client files into addon/lua/autorun/client/clientfiles.lua
or in your code that is in addon/lua/autorun/luafile.lua put
[CODE]if SERVER then
-- Server shit goes here
end
if CLIENT then
-- Client shit goes here
end[/CODE]
[QUOTE=FlyPiggyBanks;51493764]Either put your server files in addon/lua/autorun/sever/serverfiles.lua
and client files into addon/lua/autorun/client/clientfiles.lua
or in your code that is in addon/lua/autorun/luafile.lua put
[CODE]if SERVER then
-- Server shit goes here
end
if CLIENT then
-- Client shit goes here
end[/CODE][/QUOTE]
Ohhh, okay. I just had all my files in /gamemodes/pvp/gamemode, as thats what the Garry's Mod wiki said to do, is that not the right way to do it?
[QUOTE=Iceotty;51496752]Ohhh, okay. I just had all my files in /gamemodes/pvp/gamemode, as thats what the Garry's Mod wiki said to do, is that not the right way to do it?[/QUOTE]
It is the right setup.
you should have
/gamemodes/pvp/gamemode/init.lua (Serverside code, only server runs it.)
/gamemodes/pvp/gamemode/cl_init.lua (Clientside code, only client runs it.)
/gamemodes/pvp/gamemode/shared.lua (Code shared on server and client. Normally you setup your teams in there)
[QUOTE=Marmigamed;51498940]It is the right setup.
you should have
/gamemodes/pvp/gamemode/init.lua (Serverside code, only server runs it.)
/gamemodes/pvp/gamemode/cl_init.lua (Clientside code, only client runs it.)
/gamemodes/pvp/gamemode/shared.lua (Code shared on server and client. Normally you setup your teams in there)[/QUOTE]
Aha, okay thanks. That helps.
Sorry, you need to Log In to post a reply to this thread.