InputBegan:Wait() - Turning off Extra Input

Hi all! I am building a custom inventory system, and in order to assign tools to a toolbar slot, I am calling var = UserInputService.InputBegan:Wait(). This yields the current thread and assigns the user’s next input to a variable. Users press one of the number keys, 1 through 6, to assign it to a hotkey slot.

If a user attempts to assign a tool to a slot which is already taken, my internal notification system tells them that they are unable to do that and cancels the assignment. This behaviour works as expected.

However, if there is already a tool in that slot, pressing that number key equips the tool as well. This is because I have bound the number keys using ContextActionService to allow users to use/put away the tool.

I have looked through the documentation, but cannot find much mention of the :Wait() method being used with InputBegan, and thus no mention of this issue where waiting for an input does not stop that input’s regular functions from firing.

How can I prevent the regular functions from firing while I have the InputBegan:Wait() method triggered?

The flashlight (or whatever tool is there at the time) equipping is the unwanted behaviour. The #2 key is bound to taking out/putting away the flashlight, but I would like my script to ignore this bind while the :Wait() method is getting an input.

Your help would be appreciated!

1 Like

If you want to yield while ignoring certain keybinds then you have to make a connection and use the couroutine library

local running = coroutine.running()

local continueOnKey = {Enum.KeyCode.X}
local con con = game:GetService("UserInputService").InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	if not table.find(continueOnKey, inp.KeyCode) then return end
	
	con:Disconnect()
	coroutine.resume(running)
end)

coroutine.yield()

Altough yielding might not be the best solution in your case
It could just be better to run whatever you want in the inputBegan function

I am not looking to yield other parts of the script, only attempting to prevent the other keybinds I’ve set (or that Roblox sets, like space as the ‘jump’ key) to not register (or at least not be acted upon) while I am waiting for my user’s input.

Are you aware of any way that I could temporarily (while waiting for input) disable those things and not the other connections in the script?

using :Wait() does yield the other code though…
I gave you exactly what you’re asking for

Thank you for your help—I do not truly understand the coroutine library and thus I am unable to really implement it here.

I have solved the issue another way.