Weird SWEP Glitch

So the following is the code I added to cl_init.lua:

[lua]
function SWEP:Initialize()
if self.CanPrimaryAttack then
hook.Add(“KeyPress”, “FOVChangeOnPress”, function(ply, key)
if (key == IN_ATTACK2) then
if (!self.FOVChanged and self.Secondary.IronFOV != 0 and !self.FOVSet) then
self.Owner:SetFOV(self.Secondary.IronFOV, 0.2)
self.FOVSet = true
self.FOVChanged = true
end
end
end)

	hook.Add("KeyRelease", "FOVChangeOnRelease", function(ply, key)
		if (key == IN_ATTACK2) then
			if (self.FOVChanged and self.Secondary.IronFOV != 0 and self.FOVSet) then
				self.Owner:SetFOV(0, 0.2)
				self.FOVSet = false
				self.FOVChanged = false
			end
		end
	end)

	hook.Add("KeyPress", "FOVChangeOnRunPressed", function(ply, key)
		if (self.FOVChanged and key == IN_SPEED) then
			if (self.Secondary.IronFOV != 0 and self.FOVSet and FOVChanged) then
				self.Owner:SetFOV(0, 0.2)
				self.FOVSet = false
				self.FOVChanged = false
			end
		end
	end)

	hook.Add("KeyPress", "FOVStayOnAttackPressed", function(ply, key)
		if (self.FOVChanged and key == IN_ATTACK) then
			if (!FOVChanged and self.Secondary.IronFOV != 0 and !self.FOVSet) then
				self.Owner:SetFOV(self.Secondary.IronFOV, 0.2)
				self.FOVSet = true
				self.FOVChanged = true
			end
		end
	end)
end

end
[/lua]

Yes, “SWEP.FOVChanged = false” is in shared.lua

Please, no one suggest that I put this in shared. I want the weapon to run as smoothly as possible.

So, every time I shoot… The FOV resets, even whenever I’m holding IN_ATTACK2. Please help!

NOTE: I only have to press IN_ATTACK, I do not need to release it.

NOTE #2: No errors are being displayed.

Anyone know how to fix this?

Guys, I really need this. Please help me out!

Seriously? No one at all? Lol

try putting this outside of the
function SWEP:Initialize()
function

local FOVChanged = false

other then that you will have to explain what is going on better then you have

I’ve added it to shared.lua

“SWEP.FOVChanged = false”

Also, I’m trying to change the fov when the person presses attack2, and when they release attack2 it should reset the fov. But I want it clientside instead of shared.

(pls dont blame me if im wrong)

You have to use self.FOVChanged if you set SWEP.FOVChanged instead of FOVChanged

I think the problem might be that you’re creating so many keypress and keyrelease hooks at once when you could just be using one and putting all the code in one hook - something is probably getting overwritten or is being run in the wrong order

Also, I think your hooks might get overwritten unless you use self as the unique identifier of them

Maybe you could use the “PlayerBindPress” hook.

Well, I’ve only had one keypress and keyrelease hooks… It still didn’t work.

Also, I’m confused on what you mean when you say “use self as the unique identifier of them”.

[editline]26th March 2016[/editline]

This was wrong, you can set a variable instead. However, setting all your main variables are convenient if you do it in shared.lua

But, you made me think about doing that so I did. It’s pretty convenient.

Well, I just mean the second argument for the hook- e.g.



hook.Add("KeyPress", "FOVChangeOnPress", function(ply, key)


The second argument you gave was “FOVChangeOnPress”. If another hook was created with that identifier, it’d get overwritten and it wouldn’t work, so that’s why I think using ‘self’ - meaning the weapon - would be a good idea (you can use anything for that identifier)

There’s only one problem with using ‘self’ in your code though- since you’re creating multiple hooks that have the same event for them, they’d all overwrite themselves, so that’s why I think it’d be a good idea to make them all one

On a fundamental level, why are you using gamemode hooks in a SWEP? They would run even when you have a different weapon equipped.

Since you’re setting FOV, all you need to do is define the SWEP’s

WEAPON/TranslateFOV hook or

WEAPON/CalcView.

What’s he supposed to do if he wants to check when a player’s pressing W?

This is just making me more confused lol.

Would I use swep hooks instead?

Player:KeyDown +

Player:KeyDownLast

Or store the first time KeyDown was found true as a variable like self.start_forward = CurTime() (and obviously reset it when keydown is false)

It’d be more ideal- you could use

WEAPON/PrimaryAttack for the IN_ATTACK bit and

WEAPON/SecondaryAttack, but then there’s still the problem of having a hook for the IN_SPEED thing - but you could probably do a keydown check in

WEAPON/Think to take care of that

[editline]27th March 2016[/editline]

a little bit ninja’d

so, I now have the following code:

[lua]
function SWEP:Think()
hook.Add(“KeyPress”, “FOVChangeOnRunPressed”, function(ply, key)
if (self.FOVChanged and key == IN_SPEED) then
if (self.Secondary.IronFOV != 0 and self.FOVSet and FOVChanged) then
self.Owner:SetFOV(0, 0.2)
self.FOVSet = false
self.FOVChanged = false
print(“imrunninlel”)
end
end
end)
end

function SWEP:SecondaryAttack()
hook.Add(“KeyPress”, “FOVChangeOnPress”, function(ply, key)
if (key == IN_ATTACK2) then
if (!self.FOVChanged and self.Secondary.IronFOV != 0 and !self.FOVSet) then
self.Owner:SetFOV(self.Secondary.IronFOV, 0.2)
self.FOVSet = true
self.FOVChanged = true
print(“hi”)
end
end
end)

hook.Add("KeyRelease", "FOVChangeOnRelease", function(ply, key)
	if (key == IN_ATTACK2) then
		if (self.FOVChanged and self.Secondary.IronFOV != 0 and self.FOVSet) then
			self.Owner:SetFOV(0, 0.2)
			self.FOVSet = false
			self.FOVChanged = false
			print("bye")
		end
	end
end)

end

function SWEP:PrimaryAttack()
if (self.FOVChanged and self.Secondary.IronFOV != 0 and !self.FOVSet) then
self.Owner:SetFOV(self.Secondary.IronFOV, 0.2)
self.FOVSet = true
self.FOVChanged = true
print(“shootinglel”)
end
end
[/lua]

For some reason, none of the print statements are being executed. I’m so confused right now lol.

I didn’t mean to create the hooks in those functions, I meant for you to get the code you’re putting in the hooks to run in the functions there - nevermind though, I didn’t notice you had a KeyRelease hook, so it wouldn’t have worked anyway, so I guess you’ll have to undo all that stuff so it was the same as how you originally did it (sorry for making you do that, I just didn’t notice you had a different hook, and also I don’t actually know what the problem is :ohno:)

Fuck… This is weird lol

uhh why would the keyrelease hook make any difference? All this stuff should still be in the SWEP hooks.

I’m on mobile right now so I can’t type up an example, but I’ll try and explain what I meant as soon as I get on a PC.

[editline]28th March 2016[/editline]

If this is a double-post, could someone please explain to me how and when they get auto-merged? If this one is merged then, good :stuck_out_tongue:

On topic:
[lua]function SWEP:SecondaryAttack()
self.TargetFOV = self.Secondary.IronFOV
end

function SWEP:TranslateFOV(oldfov)
self.CurrentFOV = self.CurrentFOV or oldfov end
self.TargetFOV = self.TargetFOV or oldfov end
self.LastFOVTime = self.LastFOVTime or CurTime() end

if self.TargetFOV == 0 then self.TargetFOV = oldfov end

if !self.Owner:KeyDown(IN_ATTACK2) then self.TargetFOV = oldfov end	-- Player let go of secondary fire

local FOVChangeSpeed = 1	-- tweak this 1 until you get a nice result
local changetime = CurTime() - self.LastFOVTime
local allowedchange = changetime * FOVChangeSpeed

self.CurrentFOV = math.Approach(self.CurrentFOV, self.TargetFOV, allowedchange)
self.LastFOVTime = CurTime()

return self.CurrentFOV

end[/lua]

This doesn’t handle sprinting - I can’t do all the work for you :wink:
Good luck!

Edit: changed to use

math.Approach, when I realized there’s a function for it. What I did before that was a manual implementation of math.Approach.