Running Server-side Lua from the share.lua

I currently have this in the weapon swep,


function SWEP:SecondaryAttack()
   
   self:EndMusic()

   	if self:CanSecondaryAttack() then
		self.Weapon:SetNextSecondaryFire(CurTime()+0.2)
		self.Weapon:SetNextPrimaryFire(CurTime()+1)
		if CLIENT then
			local ply = self.Owner
			local scrW, scrH = ScrW(), ScrH()
			local w,h = scrW/6, scrH/4
			local x,y = (scrW/2)-(w/2), (scrH/2)-(h/2)
			
			local bx, bw, bh = (w/16), w-(w/8), 25
			
			local function BH(index)
				return ( ((bh+3)*index) )
			end
			
			local master = vgui.Create( "DFrame" )
			master:SetSize( w, h ) -- Set the size of the panel
			master:Center()
			master:SetTitle( "Choose Song" )
			master:SetDraggable( true )
			master:SetSizable(false)
			master:SetScreenLock(true)
			master:MakePopup()
			
			local clas = vgui.Create("DButton")
			clas:SetParent( master )			
			clas:SetText( "Classical" )				 
			clas:SetPos( bx, BH(1) )					
			clas:SetSize( bw, bh )				 
			clas.DoClick = function()		
				master:Close()
				self:Remove()
				ply:Give( "weapon_dubstepgun_clas" )
			end
		
			local jazz = vgui.Create("DButton")
			jazz:SetParent( master )			
			jazz:SetText( "Jazz" )				 
			jazz:SetPos( bx, BH(2) )					
			jazz:SetSize( bw, bh )				 
			jazz.DoClick = function()		
				master:Close()
				self:Remove()
				ply:Give( "weapon_dubstepgun_jazz" )
			end
			
			local pop = vgui.Create("DButton")
			pop:SetParent( master )			
			pop:SetText( "Popstar" )				 
			pop:SetPos( bx, BH(3) )					
			pop:SetSize( bw, bh )				 
			pop.DoClick = function()		
				master:Close()
				self:Remove()
				ply:Give( "weapon_dubstepgun_ps" )
			end
			
			local sim = vgui.Create("DButton")
			sim:SetParent( master )			
			sim:SetText( "Escape The Simulator" )				 
			sim:SetPos( bx, BH(4) )					
			sim:SetSize( bw, bh )				 
			sim.DoClick = function()		
				master:Close()
				self:Remove()
				ply:Give( "weapon_dubstepgun_ests" )
			end
			
			local okt = vgui.Create("DButton")
			okt:SetParent( master )			
			okt:SetText( "Oktoberfest" )				 
			okt:SetPos( bx, BH(5) )					
			okt:SetSize( bw, bh )				 
			okt.DoClick = function()		
				master:Close()
				self:Remove()
				ply:Give( "weapon_dubstepgun_oktoberfest" )
			end
			
			local ret = vgui.Create("DButton")
			ret:SetParent( master )			
			ret:SetText( "Retro" )				 
			ret:SetPos( bx, BH(6) )					
			ret:SetSize( bw, bh )				 
			ret.DoClick = function()		
				master:Close()
				self:Remove()
				ply:Give( "weapon_dubstepgun_ret" )
			end
			
			local dmet = vgui.Create("DButton")
			dmet:SetParent( master )			
			dmet:SetText( "Death Metal" )				 
			dmet:SetPos( bx, BH(7) )					
			dmet:SetSize( bw, bh )				 
			dmet.DoClick = function()		
				master:Close()
				self:Remove()
				ply:Give( "weapon_dubstepgun_dm" )
			end
		end
	end
   
   return true
end

And I need to be able to call the Give and Remove functions as they are separate weapons, however the GUI must be draw client side and the Give/Remove must be run server side. Any suggestions?

If it’s shared you can use:



if SERVER then

end

if CLIENT then

end



Alternatively, you can split the file sup into init.lua, cl_init.lua and shared.lua.

Hi, I would do it with a net.Start() function where I write the weapon class string and player, and send to server. Then handle the give and remove on server side.
If the SWEP is not part of a gamemode, I would create a function inside the SWEP and call it with the arguments weaponclass and player and put the Give and remove into an “if SERVER then” statement.

Hope it helps.

Here’s how I’d do it:



if SERVER then
	util.AddNetworkString("give_weapon_dubstepgun")
end

local allowedTypes = {"clas","jazz","ps","ests","oktoberfest","ret","dm"}
net.Receive("give_weapon_dubstepgun", function(len, ply)
	local type = net.ReadString()
	if not table.HasValue(allowedTypes, type) then return end
	local weapon = "weapon_dubstepgun_" .. type
	ply:Give(weapon)
end)


Then in each button, you would just need to do ex:



net.Start("give_weapon_dubstepgun")
net.WriteString("clas")
net.SendToServer()