Figuering out the think hook.

Hey, i need a think hook in a swep (because npc’s dont use the normal SWEP:Think()) but i dont know how to get the current owner/ weapon. So basicly how do i get the current weapon/owner in the


hook.Add("Think", "NPCThink", function()end)

think hook that has been created in a swep?

:wtc:

First your talking about NPCs then SWEP’s

Explain more clear what you want to do

Sorry… A swep cant use SWEP:Think() while its owner is an npc, so i need to create another think hook, but it gives me error whenever i try to get the weapon the think is created in.

If the SWEPs owner is an NPC, then run a seperate Think hook, since apparently SWEP:Think() doesn’t work on them.

:ninja:

[lua]
self.Owner:IsNPC()

self.Weapon
[/lua]

-.- i know! thats why i made this thread… To help me get another think hook properly working.

I know! That’s why I made that post that actually wasn’t directed at you!

Anyway, your weapon’s initialize:

[lua]

function SWEP:Initialize()
– blah blah

if(self.Owner:IsNPC()) then
    hook.Add("Think", "MySWEPThink"..self.Weapon:EntIndex(), self.Think)
end

end

[/lua]

Though I’m not sure if self.Weapon or self.Owner are already valid there.

Else, just set it in Deploy and remove the hook on OnRemove/Holster

Sorry about the last comment then :slight_smile: anyway… I already tried that my problem is when i try to run a simple


self.Weapon:EmitSound("Weapon_SMG1.Reload")

to see if it works i get this:


shared.lua:60: attempt to index local 'self' (a nil value)

So thats my real problem :confused:

You cannot do it like that, you would create one think hook to control all of the weapons

:smith:Any solutions?

[lua]function SWEP:Initialize()
local weapon = self;
hook.Add(“Think”, "NPCThink " … tostring(self), function()
if (weapon.Owner:IsValid() and weapon.Owner:IsNPC()) then
weapon:Think();
end
end);
end
function SWEP:OnRemove()
hook.Remove(“Think”, "NPCThink " … tostring(self));
end[/lua]
Untested.

Hey… It works! :stuck_out_tongue: thanks :slight_smile:

Ok another problem. The think hook fucks up when trying to do anything with :GetEnemy(). I used Lexic’s way and whenever i try to even check if


self.Owner:GetEnemy():IsValid() / ValidEntity(self.Owner:GetEnemy())

i get this


Hook 'NPCThink213' Failed: addons\***\shared.lua:68: attempt to call method 'GetEnemy' (a nil value)

any solutions?

Are you sure it’s ran serverside?
Are you sure you’re calling it on an NPC?

[editline]04:30PM[/editline]

Also start posting these threads in Newbie Questions.

Pretty sure, it works in the rest of the script, just not in the part where i used the NPCThink hook

[editline]07:31PM[/editline]

:frowning: ok

[editline]08:12PM[/editline]
Nvm Fixed it. the guy above me was right… I didn’t run it on server. An embarrassing mistake :confused: