Why does this print "W" or "S" three times?

UserInputService.InputBegan:Connect(function (InputObject, GameProcessedEvent)
	if not GameProcessedEvent then
		if InputObject.UserInputType == Enum.UserInputType.Keyboard then
			if InputObject.KeyCode == Enum.KeyCode.LeftShift then
				if UserInputService:IsKeyDown(Enum.KeyCode.S) then
					print("S")
				elseif UserInputService:IsKeyDown(Enum.KeyCode.W) then
					print("W")
				end
			end
		end
	end
end)

Right now, I’m trying to script a system that will make a player do something if they press shift while W or S is held down. The code I’m using is above. Assuming the print means the function is called, the function is called three times. How do I get it to run only once?

The way I set up the code is mostly intentional. Shift + S takes precedence over Shift + W.

2 Likes

I would put a wait() in there to let it cool down before being triggered again!

A debounce is intended to make its presence here, which I’m certain will solve the problem of the above running three times. I’m wondering though; is there a way to make the above fire only once without said debounce, or is there no point in knowing that?

Wait wouldn’t solve the problem; it’d just stall how long before it gets printed again.

How are you reproducing this issue? When I tested in studio it only printed once for each time I hit the LeftShift key.

I can’t answer your question, but a tip for UX:

The way you have set it up is that it will only run when shift is pressed and the keys are already down – meaning if the player pressing shift then pressing one of those other keys, it won’t run.

So to get best experience for your players you’d want to do something like this:

Code
local PressedS = false
local PressedW = false
local PressedShift = false

local function Check()
    if PressedS and PressedShift then
        -- s
    elseif PressedW and PressedShift then
        -- w
    end
end

UserInputService.InputBegan:Connect(function (InputObject, GameProcessedEvent)
	if not GameProcessedEvent then
		if InputObject.UserInputType == Enum.UserInputType.Keyboard then
			if InputObject.KeyCode == Enum.KeyCode.LeftShift then
			    PressedShift = true
                Check()
            elseif InputObject.KeyCode == Enum.KeyCode.S then
                PressedS = true
                Check()
            -- etc.
			end
		end
	end
end)
2 Likes
  • Hold W and press LShift. Prints W three times for me.
  • Hold S and press LShift. Prints S three times for me.
  • Hold W and S and press LShift. Prints S three times for me.

you need to make sure when you’re coding that you use the right functions for what you’re trying to achieve. If you have the key down then it will repeatedly print. Make sure it’s only printing for the key going down once.

That’s intentional.

1 Like

Shouldn’t this be the case though? I’m using InputBegan meaning it should only be firing once for when input starts, not twice more.

I tried the same thing and it only printed once for me. Are you sure there is only one copy of the script in your game? Otherwise this sounds like a bug, because the event should only fire once.

2 Likes

Sure as can be.

Does it still happen with the same code in StarterPlayerScripts on an empty baseplate?

Yep.


1 Like

I would recommend filling a bug report for this and using a workaround with a debounce for now.

2 Likes

I haven’t tested your code, but just a thought,

I suspect your keyboard might be sending Enum.UserInputState.Change events while you hold down, so try adding a check for InputObject.UserInputState being Enum.UserInputState.Begin

5 Likes

@TheGamer101 @colbert2677 This is the intended behaviour.
I also encountered this problem while I was an Accelerator and filed it as a bug.
It turned out that it was the event firing with the different InputStates (begin, change, end).
You can use InputObject.UserInputState to check the current state.

1 Like

@badcc Tried that before, fired three times as well.

@AlgyLacey Ohh. How come it fires three times for Begin, Change and End when I’m connecting to InputBegan though?

Yeah, I just printed UserInputState. All three times are UserInputState.Begin.

Not getting the bug, either.

Are you using some sort of special keybord? Maybe plugins are causing the bug, too.

1 Like

It’s probably a plugin. I don’t know why I didn’t do it before but I tried out the script on a live server. Only printed once, as it should.