• player:give function ?
    10 replies, posted
hi everyone, i ask to the " player:give " work ? because i have created a menu with some icons (like the original sandbox menu) and i want to give a weapon to the player when he click on it seems like this : (A CLIENT SIDE CODE) [CODE]-- Weapons Content Here ! -- local icon = vgui.Create( "ContentIcon", Frame ) icon:Center() icon:SetMaterial( "entities/weapon_pistol.png" ) icon:SetName( "Pistol" ) --icon.DoClick = function() RunConsoleCommand( "give", "weapon_pistol" ) end <= doesn't work, needs sv_cheats 1 enabled icon.DoClick = function() ply:Give( "weapon_pistol" ) end -- <= error [/CODE] when i click on this button game drop a error and i want to know why he cannot ? dont tell me this function is ServerSide ...... i will cry ... Thanks for incomming responses :v:
It's Serverside. You can go here [url]https://wiki.garrysmod.com/page/Player/Give[/url]. If it's blue it's serverside, Orange is clientside. Half and Half is shared.
[QUOTE=Matsumoto;50571337]It's Serverside. [/QUOTE] *@°+{[|é#@*:mindblown::suicide::blaze: thanks :) i have read in wide in tall in hell the wiki , i dont understand everythings ... thats why the little square is blue : for server side ! thats okay [editline]23rd June 2016[/editline] i have try this : client side code : [CODE] -- Weapons Content Here ! -- local icon = vgui.Create( "ContentIcon", Frame ) icon:Center() icon:SetMaterial( "entities/weapon_pistol.png" ) icon:SetName( "Pistol" ) --icon.DoClick = function() RunConsoleCommand( "give", "weapon_pistol" ) end icon.DoClick = function() SendToServerSpawnWeapon("weapon_pistol") end function SendToServerSpawnWeapon(weaponName) net.Start( "giveweapon", weaponName ) net.SendToServer() end) [/CODE] server side code : [CODE]util.AddNetworkString( "giveweapon" ) net.Receive( "giveweapon", function(ln, pl,weap1) ply:give(weap1) end end)[/CODE] but nothing happend
I made some random code that'll do what you want Server: [CODE] util.AddNetworkString( "giveweapon" ) net.Receive("giveweapon", function(len, ply) ply:Give("weapon_crowbar") end)[/CODE] Client: [CODE] function Test() local Frame = vgui.Create("DFrame") Frame:SetSize(1000,1000) Frame:Center() Frame:SetTitle("") Frame:MakePopup() local Test = vgui.Create("DButton", Frame) Test:SetSize(100,30) Test:SetPos(40, 100) Test:SetText("") Test.DoClick = function() net.Start("giveweapon") net.SendToServer() end end concommand.Add("vagina", Test)[/CODE]
i have try you example but ... hello to you : [CODE] [ERROR] addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:116: Calling net.Start with unpooled message name! [http://goo.gl/qcx0y] 1. Start - [C]:-1 2. SendToServerSpawnWeapon - addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:116 3. DoClick - addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:71 4. unknown - lua/vgui/dlabel.lua:218 [/CODE] i think we can't do this : [CODE] net.Start( "giveweapon", weaponName ) -- <= weaponName Sucks a lot yolo [/CODE] how to send my weapon name to my function ? :)
[QUOTE=BALIST0N;50571588]i have try you example but ... hello to you : [CODE] [ERROR] addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:116: Calling net.Start with unpooled message name! [http://goo.gl/qcx0y] 1. Start - [C]:-1 2. SendToServerSpawnWeapon - addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:116 3. DoClick - addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:71 4. unknown - lua/vgui/dlabel.lua:218 [/CODE] i think we can't do this : [CODE] net.Start( "giveweapon", weaponName ) -- <= weaponName Sucks a lot yolo [/CODE][/QUOTE] I think you should look into [URL="http://wiki.garrysmod.com/page/Net_Library_Usage"]this[/URL], it shows you how to use the net library correctly. Also the reason you're getting that error is because you have to add the name of the message to the string table. You do that using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/util/AddNetworkString]util.AddNetworkString[/url]
[CODE] function SendToServerSpawnWeapon(weaponName) print(weaponName) net.Start( "giveweapon" ) net.SendToServer() end [/CODE] [CODE]util.AddNetworkString( "giveweapon" ) net.Receive( "giveweapon", function(ln, pl) pl:give("weapon_crowbar") end) [/CODE] [CODE][ERROR] addons/gunmenu/lua/autorun/server/sv_weaponlist.lua:8: attempt to call method 'give' (a nil value) 1. func - addons/gunmenu/lua/autorun/server/sv_weaponlist.lua:8 2. unknown - lua/includes/extensions/net.lua:32 [/CODE] there a progress :cry: [url]http://wiki.garrysmod.com/page/Player/Give[/url] :weeb: [editline]23rd June 2016[/editline] [QUOTE=BALIST0N;50571679][CODE] [CODE] pl:give("weapon_crowbar") [/CODE] [/QUOTE] why i man an idiot ...? : [CODE] pl:Give("weapon_crowbar")[/CODE] Give ! and not give ..... ok so now i need to send the selected weapon : in the "print" the console show [CODE] gmod_pistol [/CODE] so ... how to send it ...?
[code] //client local function SendWeaponToServer( wep ) net.Start( "giveweapon" ) net.WriteString( wep ) net.SendToServer() end //server util.AddNetworkString( "giveweapon" ) net.Receive( "giveweapon", function( _, ply ) local wep = net.ReadString() ply:Give( wep ) end ) [/code]
Fully working code- Client- [CODE]function WeaponMenu() local Frame = vgui.Create("DFrame") Frame:SetSize(250,250) Frame:Center() Frame:SetTitle("") Frame:MakePopup() local Test = vgui.Create("DButton", Frame) Test:SetSize(100,30) Test:SetPos(40, 100) Test:SetText("") Test.DoClick = function() net.Start("giveweapon") net.SendToServer() end end concommand.Add("menuwep", WeaponMenu)[/CODE] Server- [CODE]util.AddNetworkString( "giveweapon" ) net.Receive( "giveweapon", function(len, ply) ply:Give("weapon_crowbar") end)[/CODE]
PERFECT ! THANKS ALL OF YOU ! I GO TO SLEEP NOW ! Client Side Code : [CODE]-- Weapons Content Here ! -- local icon = vgui.Create( "ContentIcon", Frame ) icon:SetName( "Pistol" ) icon:SetMaterial( "entities/weapon_pistol.png" ) icon:SetPos(100,100) icon.DoClick = function() SendToServerSpawnWeapon("weapon_pistol") end -- SEND WEAPON GIVE -- function SendToServerSpawnWeapon(weaponName) net.Start( "giveweapon" ) net.WriteString( weaponName ) net.SendToServer() end [/CODE] ServerSide Code : [CODE]util.AddNetworkString( "giveweapon" ) net.Receive( "giveweapon", function(ln, pl) local wep = net.ReadString() pl:Give(wep) end) [/CODE]
[QUOTE=BALIST0N;50571796]PERFECT ! THANKS ALL OF YOU ! I GO TO SLEEP NOW ! Client Side Code : [CODE]-- Weapons Content Here ! -- local icon = vgui.Create( "ContentIcon", Frame ) icon:SetName( "Pistol" ) icon:SetMaterial( "entities/weapon_pistol.png" ) icon:SetPos(100,100) icon.DoClick = function() SendToServerSpawnWeapon("weapon_pistol") end -- SEND WEAPON GIVE -- function SendToServerSpawnWeapon(weaponName) net.Start( "giveweapon" ) net.WriteString( weaponName ) net.SendToServer() end [/CODE] ServerSide Code : [CODE]util.AddNetworkString( "giveweapon" ) net.Receive( "giveweapon", function(ln, pl) local wep = net.ReadString() pl:Give(wep) end) [/CODE][/QUOTE] Good job!
Sorry, you need to Log In to post a reply to this thread.