How to use SetActiveWeapon

I want something like the code below;

[lua]
self.Owner:SetActiveWeapon(“weapon_pistol”)
[/lua]

But I know that it wouldn’t work, because you can’t pass a string classname to the function. How would I get this functionality?

self.Owner:SetActiveWeapon(self) ?

A lot of your questions could go into the “questions that don’t need their own thread” thread. There’s also SWEP.AutoSwitchTo, but I don’t know if that works.

Wait, sorry; I phrased the question wrong. The code will be going inside a serverside hook, and the line would look like this;

[lua]ply:SetActiveWeapon(“weapon_pistol”)[/lua]

And the reason that I don’t post inside that thread is that most of the time I never get an answer, or I get an unsatisfactory one that requires more discussion and I end up making a thread anyway. It’s generally unhelpful.

Had you searched the wiki for the word ‘weapon’ you would have found PLAYER:GetWeapon() which does take a string and gives you an entity you could then pass into SetActiveWeapon. I think that’s why you don’t always get a response, some of your questions can be answered with one or two minutes searching the wiki.

I did in fact search the wiki for ‘weapon’, and found the function

weapons.Get before the one you linked above. I tried it and it didn’t work; so I came here for answers. Please don’t assume.

I made this line using that function, and it sort of works;

[lua]ply:SetActiveWeapon(ply:GetWeapon(“swep_name”))[/lua]

Although it does change the weapon to the one selected, the viewmodel stays the same as the last weapon selected for some reason.

-snip-

Edit:

Lolcats’ is better

You know there’s Player:SelectWeapon.

ply:SelectWeapon(“hi”)

Thanks, that’s even more helpful. And it even fixes the viewmodel problem.