Weapons and a gamemode

Is there anyway a swep can call a function from a gamemode like I write a function with some checking inside and the swep calls it.

More information is necessary. Client side can call client side or shared Lua. Server side can call server side and shared Lua. What are you exactly trying to call because depending on the situation you would have to use console commands/datastream which datastream is just a fancier way of using console commands.

Its serverside, I’m trying to make some sort of harvesting swep.

Yep, entirely possible. If you define a function in the serverside gamemode, you can call it like any other function in the SWEP, as long as the call is in the SWEP’s init or shared.


For some reason this does not work, anyone care to explain ?

Swep : shared

SWEP.Author = “Xyro”
SWEP.Contact = “”
SWEP.Purpose = “Harvest”
SWEP.Instructions = “Left click to Harvest”

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/weapons/v_crowbar.mdl”
SWEP.WorldModel = “models/weapons/w_crowbar.mdl”

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = “none”

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”

local ShootSound = Sound( “Weapon_Crowbar.Single” )

Reload does nothing
function SWEP:Reload()

Think does nothing
function SWEP:Think()

function SWEP:PrimaryAttack()

self.Weapon:EmitSound( ShootSound )
self.Owner:SetAnimation( PLAYER_ATTACK1 );
self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER );

local pos = self.Owner:GetShootPos()
local ang = self.Owner:GetAimVector()
local tracedata = {}
tracedata.start = pos
tracedata.endpos = pos+(ang*200)
tracedata.filter = self.Owner
local trace = util.TraceLine(tracedata)
if not trace.Hit then return end

entity = trace.Entity
if not entity:IsValid() then return end 

physicsObj = entity:GetPhysicsObject()
if not physicsObj:IsValid() then return end

self:DoProcces(self.Owner, "harvest", entity)


function SWEP:SecondaryAttack()


function SWEP:Initialize()


Gamemode init :

function DoProcces(ply, procces, entity)



In what directories did you place the files?

garrysmod/gamemode/waterworld/entities/weapons weapon
garrysmod/gamemode/waterworld for the gamemode

oh lol I forget to include the error :

weapons\harvesting_tools\shared.lua:62: ‘=’ expected near ‘end’

Ah that will help.
try adding a = sign before the end as the error does say that if you read it.
or try remove the end.

sorry for this post it seems that while testing I put a seccond file inside the garrymod/lua folder so it was trying to use a old file xD. But now a seccond question It gives this error

weapons\harvesting_tools\shared.lua:60: attempt to call method ‘DoProcces’ (a nil value)

I think its because you hav’nt coded it enough for it to understand what you want to do, the code you have on line 60 is meaningless

The swep needs to know what harvesting is. So you must code it.

Huh Im only trying to call a function from the sweps primary attack the code in the gamemode file is only a test :O.

and the code on line 60 is supposed to call it, if not how do I call it.

If it’s a gamemode function, you’re calling it from the wrong place. In the SWEP,

[lua]self:DoProcces(self.Owner, “harvest”, entity) --This will call the function DoProcces in the weapon’s table. Examples of functions in the weapon’s table: PrimaryAttack, Reload[/lua]

If you want to call a gamemode function that you made in the gamemode table (e.g. function GM: …),

[lua]GAMEMODE:DoProcces(self.Owner, “harvest”, entity) --This will call the function DoProcces in the gamemode table.[/lua]

Thanks, I thought something like that but I couldn’t find something like that on Gmod Wiki


For some reason It still gives the same error

weapons\harvesting_tools\shared.lua:60: attempt to call method ‘DoProcces’ (a nil value)

Im using this :

GAMEMODE:DoProcces(self.Owner, “harvest”, entity)

Do I need to declare what my gamemode is first ?
Its derived from base

Wait a minute :sigh:

I believe you spelt DoProcess as DoProcces .

No instead just doing

[lua]function DoProcces() [/lua]


[lua]function GM:DoProccess()[/lua]

It worked thanks !