Problems with using hooks within STools

Hi all,

To put it simply, the way the gmod_tool SWEP and ToolObj objects are being created seems to make using external hooks very difficult.

As an example, say I have a tool that needs to use the **EntityTakeDamage **hook. Let’s say I also need the gmod_tool SWEP and TOOLObj to be in the scope of this hook because I’d like to check a cvar on the tool’s owner to see if any arbitrary entity should take damage or not.

I have to use a function like TOOL:Init() to create the hook when the tool SWEP is valid and within the scope of the hook, so I’ll end up with something like this:

-- ...

TOOL.ClientConVar[ "damage" ] = "0"

if ( SERVER ) then
    function TOOL:Init()

    function TOOL:SetupEntityTakeDamage()
         hook.Add( "EntityTakeDamage", "example", function( ent, info )
                 if ( self:GetClientNumber( "damage", 0 ) == 0 ) then
                         info:SetDamage( 0 )
         end )

All is fine and well (besides this being a bad example), until the player dies or loses the gmod_tool SWEP. As soon that happens, the gmod_tool SWEP is **removed **causing calls like self:GetClientNumber( “somecvar” ) to break.

Basically every prop in this example suddenly becomes invincible regardless of the owner’s cvar setting because:
EntityTakeDamage is trying to call self:GetClientNumber() -> which calls self:Owner() -> which tries to index the now NULL self:GetSWEP() entity, causing the unfortunate Tried to use a NULL entity! error.
(see garrysmod\garrysmod\gamemodes\sandbox\entities\weapons\gmod_tool\stool.lua line 105)

I can’t simply move the hook out of the scope of the TOOLObj because the hook relies on the owner’s cvar settings. But since the gmod_tool SWEP gets removed, I have no way of getting the tool’s owner (that I know of).

Am I missing some way to handle this issue, or is literally every external hook that relies on the tool’s owner cursed to break?

I’m not exactly sure what you are trying to do, but I always find it helpful looking at other people’s examples.
ACF stores information on an entity with duplicator.StoreEntityModifier. I’m not even sure this will work, just taking a blind guess. (aka attempting it now will post with results.)

EDIT: so with this code

I was able to save a value to the table and get it back

so, maybe you can change your tool to save whether it’s invulnerable and update if the user changes their cvar. You can either use net functions and cvar.AddChangeCallback to see when the users changes the value to update the information on the server regardless of whether or not the user has the tool.