Key:byte() not working after unequipping

What am I trying to do? I’m trying to make a gun aim in and out but tapping a key to aim the gun (which I initially figured out until I edited a small thing which I fixed but it’s still not working)
So I’m trying to fix why Key:byte() isn’t working. Key:byte() works for a second then acts like a pressed the original key again.
https://gyazo.com/38f4ef4547303a235f5473357f3ea9d6
I posted that gif to show what happens after I unequip the gun. (Under this text should be what happens when I first equip it)
https://gyazo.com/312ee9788e6f3b437c6e939bf8083248

So the problem prevails the first time, you unequip the gun and then the problem is gone? or am I mis interpreting the problem?

Please re-read what I said. (I said the first clip I posted happens after I unequip it.)

so you want to be able to tap a key and it narrows the aim down and zooms but when you first use the gun that isn’t a problem? then unequipping and re-equipping it causes the problem?

Let me rephrase it if I didn’t say it clear enough.
I meant to say the gun can’t zoom out when im aiming after I unequip the gun. So yeah I think you got it.

Since you aren’t sharing any code, there’s not much to analyze here except that Mouse.KeyDown is deprecated, you should be using UserInputService instead.

Ill share my code but I’m not using UIS because KeyDown has worked fine for me.

elseif string.lower(Key) == "q" then
				if Humanoid.WalkSpeed < 8 then
					if Debounce == false then 
						Debounce = true
						GunHoldPlay:Stop() 
						IdlePlay:Play()
						Aim()
						wait(0.1)
						Debounce = false
					end
                                end
			end
		elseif Key:byte() == 113 then
			if Debounce == false then
					Debounce = true
					GunHoldPlay:Play() 
					IdlePlay:Stop()
					AimStop()
				end
			end
		end
        end
end

(The translation from roblox studio to here broke the code so it looks messy.)

The second part of the if statement won’t ever get reached, since it’s the exact same expression. If the key is q, then the key:byte() will also always be q (113).

Thats what I’m saying it only can’t reach it after I unequip the tool and then re-equip it.

Are you unbinding the KeyDown connection when unequipping the tool?

what do you mean by ‘unbinding’?

I assume you do KeyDown:Connect in your equip event. You have to save the connection to a global variable like

local con
tool.Equipped:Connect(function(m)
con = m.KeyDown:Connect(...) 
--...

tool.Unequipped:Connect(function()
if con then
con:Disconnect()
con = nil
end
--...
1 Like

No, I don’t do anything like that at all but I’ll see what it does.

I just tested it and it fixed my weapon! Thanks dude.