Function triggering only when pressing the wrong key

So i was writing some lines of code and it all worked fine until this point. So basically what happens is that instead of the function being triggered after pressing/letting go of the R Key it doesn’t trigger, instead it’s fired by pressing the wrong key, I don’t see what’s wrong with the code and i’m starting to think the code was reversed to some point but it doesn’t show it to me… Heres some of the code:


function hpcharge() -- charge function
	local bools = Player.Bools
	local chargeval = bools.HeavyPunchCharge
	
	if chargeval.Value > 0 and chargeval.Value < 11 then
		if hpcharging == false then
			hpcharging = true
			spawn(function()
				wait(5)
				if hpcharging == true then
					hpcharging = false
				end
			end)
			while wait(.5) and hpcharging == true and chargeval.Value > 0 do
				if chargeval.Value < 11 then
					chargeval.Value = chargeval.Value + 1
					if chargeval.Value == 10 and hpcharging == true then
						hpcharging = false
						hpcharging:FireServer()
					end
				end
			end
		end
	end
end

function hprelease() -- release function
	local bools = Player.Bools
	local chargeval = bools.HeavyPunchCharge
	
	if hpcharging == true and  chargeval.Value > 0 then
		hpcharging = false
		hpremote:FireServer()
	end
end

UIS.InputBegan:Connect(function(input,IsTyping)
	if IsTyping then
		return
	elseif input.KeyCode == Enum.KeyCode.R then
		if Player.Character:FindFirstChild("StarPlatinum") then
			hpcharge() -- function is triggered after pressing the key
		end
	end
end)

UIS.InputEnded:Connect(function(input,IsTyping)
	if IsTyping then
		return
	elseif input.KeyCode == Enum.KeyCode.R then
		if Player.Character:FindFirstChild("StarPlatinum") then
			hprelease() -- function is triggered after letting go of the key
	
		end
	end
end)

I don’t know what’s wrong with the code, if im doing something wrong or anything because it only triggers when the player presses E which weirdly is another function triggering event, but there is nothing related to the lines of code above on it. Can someone please review the code and tell me what’s wrong?

Have You Tried Using UserInputService

I am not sure why you are using elseif here?

And here?

The conditions are entirely separate and not related. Try

if IsTyping then
    return
end

if input.KeyCode == Enum.KeyCode.R then
    -- ...
end

Sorry for not putting it on the visible code but i localized UserInputService as UIS. So yes, i am using UserInputService

I do not think that is the case since the press E keycode also uses elseif input.Keycode == Enum.Keycode.E then

i will still try it though.

Basically what are you trying to do when you press R something happens

Yeah, but instead its being triggered after pressing the wrong key.

before returning

you should do

if input.KeyCode == Enum.KeyCode.E then
      --basically your code
end

Nono, you got it mixxed up, what i’m trying to find a solution for is the functions not being triggered by pressing R (what i’m trying to make it do) but instead it is triggered by pressing the wrong key (E). That is the problem, everything works fine on the Enum.Keycode.E…

I tried and no good results… (30chrs)

this is because

of the parameter

IsTyping

you should use

UIS.InputBegan:Connect(function(input)
      if input.KeyCode == Enum.KeyCode.R then
             -- code
      end

end)

do that with .InputEnded

you will have to restrict the function with R

It is not because of the parameter “IsTyping” I have another part of the code that works just fine with said parameter…

i want to see that other code

30char

Here’s the the code that i’m talking about:


function barragestart()
	if Character:FindFirstChild("StarPlatinum") then
		if bardeb == false and baractive == false then
			bardeb = true
			candesummon = false
			
			barremote:FireServer(baractive)
		end
	end
end

UIS.InputBegan:Connect(function(input,IsTyping)
	if IsTyping then
		return
	elseif input.KeyCode == Enum.KeyCode.E then
		if Player.Character:FindFirstChild("StarPlatinum") then
			barragestart()
		end
	end
end)

This works completely fine, but it triggers the other function… there is nothing related to that other function though… that’s what im confused about…

Try printing out the input.Keycode, it almost seems like you remapped R. It is somewhat illogical.