Players can steal my Nextbot's gun

So i have a nextbot that holds a gun, but the problem is, if he comes into close range of myself or another player, they can take the bot’s weapon from his hands, as if it were a dropped weapon on the ground. Because my bot’s code needs the weapon to reference in order to attack, so it derps up.

I can override this by allowing it to spawn a weapon each time, but it still cannot come close to the player without losing it again, causing it to endlessly chase someone, lose it’s weapon, spawn another, and repeat.

PlayerCanPickupWeapon ?

i tried that, and i must be doing something wrong with it, because i get the error: “Attempt to index global ‘GM’ (a nil value).”

Am i supposed to place it somewhere specific?

use hook.Add

Humble is right. Any gun that is about to be picked up goes through PlayerCanPickupWeapon. Even when a weapon is ply:Give( )'ed.

Depending how the weapon is being held by the bot, you could check to make sure the weapon is valid, and the weapon is not parented to an entity, or doesn’t have some var attached to it.

hook.Add( "PlayerCanPickupWeapon","Something", GM:PlayerCanPickupWeapon )

am i doing it right?
This gives me an error

If you’re coding a gamemode, GM:functionname() functions go in code that runs while the gamemode is initializing (such as the init.lua of the whole gamemode).

Many gamemode functions are also gamemode hook functions. Such as PlayerCanPickupWeapon.

If you want to add code to control gamemode hook function behavior, you can override the gamemode’s default definition by creating a hook to it, at any time.

–your code here

[editline]29th July 2014[/editline]

oops i opened this tab 30 minutes ago

[editline]29th July 2014[/editline]

looks like i was still helpful, though

this seems to fix the problem.

[editline]29th July 2014[/editline]

well now, if i die, can’t use any weapons at all

i get an error if i try and give myself a weapon:

[ERROR] addons/nextbot/lua/entities/cp_nextbot.lua:118: Tried to use a NULL entity!
  1. GetClass - [C]:-1
   2. v - addons/nextbot/lua/entities/cp_nextbot.lua:118
    3. unknown - lua/includes/modules/hook.lua:84
     4. Give - [C]:-1
      5. unknown - gamemodes/sandbox/gamemode/commands.lua:708
       6. unknown - lua/includes/modules/concommand.lua:69

^^ any help? this is kind of frustrating, now My Bot works well, but it screws with the rest of the game.

Try doing this after parenting the weapon to your nextbot. No hooks required.


ok, this solves the problem, thanks