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() self:SetupEntityTakeDamage() end function TOOL:SetupEntityTakeDamage() hook.Add( "EntityTakeDamage", "example", function( ent, info ) if ( self:GetClientNumber( "damage", 0 ) == 0 ) then info:SetDamage( 0 ) end end ) end 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?