How to apply function to mouse3 for swep

I’m currently working on a swep that has three attacks. I’ve finished the first two and applied them to left and right click (obviously) and I’d like to know if it’s possible to apply the third attack to m3 (middle mouse button) and how to do it, if so

It would really be helpful if you posted your code too, just to get a better idea, but I think I know what you’re looking for. I think you want something like SWEP.PrimaryAttack, but for the middle mouse button, I don’t believe that’s possible. Primary and Secondary are only for left and right click. So, we’re going to have to get a bit “creative” if you want to use more keys.

You might wanna create a function and check if a key is pressed. This is what the middle mouse button is:

[lua]MOUSE_MIDDLE[/lua]

So, you’d do something like:

[lua]function getdatbuttonpress ( )

print ( "HEY, this shit returns true if you pressed middle mouse button! " … tostring ( input.IsMouseDown (MOUSE_MIDDLE) ) )

end
hook.Add (“Think” , “fdsafsas” , getdatbuttonpress) --Yes, I know… It’s for an example.[/lua]

Open your console and you’ll see it. It won’t be hard to miss (as it runs every second) then while it’s open press the middle mouse button and it’ll return true.

I don’t do many SWEP’s, so I can’t actually “make” you a function, but I think you get the idea. Just make a function, check if the button is pressed, and there you go. Used that as an example.

Do you know if there’s some way I can make it so the function doesn’t run every frame? Like using SWEP.SetNextPrimaryFire()?

Just to clarify, that above function was just an example to get you started so you could see the MOUSE_MIDDLE in use.

I’m cautious of giving you advice as I don’t normally make SWEP’s, but since no one else wants to help I’ll give it a shot. Keep in mind there’s a lot of various “key” presses and ways to do it.

You could do something like:

This will kill the player when he presses the R key. You could also make a console command and run it when someone presses a button.
[lua]
hook.Add ( “KeyPress” , “fsdfsasaf” , function ( ply , key )
if key == IN_RELOAD then
ply:ConCommand (“kill”)
print( “You pressed the " …key… " key.” )
end
end )
[/lua]

I don’t believe that you can do that with SWEP, but you could try to hook “SetNextPrimaryFire” I can’t test it myself right now. So, like: (You can play around with, IsMouseDown, too)
[lua]
hook.Add ( “SetNextPrimaryFire” , “somethinggfdsaf”, function ( ply, key )
[/lua]

If you want something simple you can do:

This will kill the player when he presses a key.
[lua]function anykey ( ply, key )
ply:ConCommand (“kill”) --Replace this with your own console command if you want.
print ("Key pressed: " …key)
end
hook.Add ( “KeyPress” , “fdsaf” , anykey )[/lua]

I’ve also had issues with KeyPress and IsMouseDown, all those things. There’s a lot of them and it’s tricky at times, but I hope this helps you out. That’s why I stick to GM:ShowHelp, ShowSpare1, ShowSpare2, ShowTeam, etc. That’s of course with the F1-F4 keys so that’s not what you’d like to do.

timer.Create?

[lua]
function KeyPress()
ply=LocalPlayer()
if input.IsKeyDown(KEY_V) && waspressed == false then
if !ply:IsTyping( ) then
RunConsoleCommand(“ulx”, “noclip”)
print(“HI”)
waspressed=true
end
elseif !input.IsKeyDown(KEY_V) then
waspressed=false
end
end
hook.Add(“Think”,“Keypresssss”,KeyPress)
[/lua]

How I enabled toggling for ulx noclip.

Alright… here ya go
[lua]
function SWEP:SetNextMiddleFire(time)
self.middledelay = time
end
function SWEP:Think() --If you already have a think function, MERGE!
–Replace with input.WasMousePressed( MOUSE_MIDDLE ) if you want it semi-auto
if input.IsMouseDown( MOUSE_MIDDLE ) and self.middledelay <= CurTime() then
–SHOOT DA SHIZ
SWEP:SetNextMiddleFire(CurTime()+0.1) – <-- MAKE SURE YOU PUT THIS IN YOUR INIT FUNCTION! Or you will have script errors!
end
end
[/lua]

This should work.

Need more help?

Sorry for the delay, had a busy weekend with friends I hadn’t seen in a while.
LegoGuy, I followed your instructions accordingly and this error floods the console when the weapon is equipped

[ERROR] lua/weapons/vayne.lua:100: attempt to index global ‘input’ (a nil value)

  1. unknown - lua/weapons/vayne.lua:100

Have come across it a few times in previous projects but I (obviously) don’t know how to fix it :expressionless:

Here’s my code

[lua]function SWEP:SetNextMiddleFire(time)
self.middledelay = time
end

function SWEP:Think()
if input.IsKeyDown( MOUSE_MIDDLE ) and self.middledelay <= CurTime() then
self:EmitSound(self.WeaponSounds2[math.random(1,#self.WeaponSounds2)], 100, 100)

RunConsoleCommand("+duck")
RunConsoleCommand("+forward")
self.Owner:SetWalkSpeed(1500)

self.Owner:ViewPunch( Angle( 60, 0, 20 ) )
self:SetNextSecondaryFire( CurTime() + 3 )

timer.Create( "Name", .4, 1, function() stoptumble() end )
timer.Create( "Name1", .4, 1, function() self.Owner:SetWalkSpeed(250) end )

end
end[/lua]

The ‘input’ library is clientside only.

It looks like you want it serverside, though. For that, you will need to use player:KeyDown().

Screwed around with it a bit more and now I get…

[ERROR] lua/weapons/vayne.lua:100: attempt to compare nil with number

  1. unknown - lua/weapons/vayne.lua:100

…when I walk forward (only forward) or left click.

[lua]function SWEP:SetNextMiddleFire()
self.middledelay = CurTime() + 3
end

function SWEP:Think()
if self.Owner:KeyPressed( MOUSE_MIDDLE ) and self.middledelay <= CurTime() then
self:EmitSound(self.WeaponSounds2[math.random(1,#self.WeaponSounds2)], 100, 100)

RunConsoleCommand("+duck")
RunConsoleCommand("+forward")
self.Owner:SetWalkSpeed(1500)

self.Owner:ViewPunch( Angle( 60, 0, 20 ) )

timer.Create( "Name", .4, 1, function() stoptumble() end )
timer.Create( "Name1", .4, 1, function() self.Owner:SetWalkSpeed(250) end )

end
end[/lua]

Thanks for your cooperation, guys, I’m lost in every sense of the word.