Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i disable keyboard input?

Is there a way to disable all keyboard input so that the players can't do anything when they're in the middle of an action?

1 answer

✓ Accepted answer

Hi Soru,

I would recommend just making a debounce affect. Every time an action is called, just check if any other actions are enabled. For this, I would recommend using Boolean Values or Variables. Also, if you want to restrict player movement, you can do that using ContextActionService. You bind the freezeMovement action to the player, and the player won't be able to move until you unbind it.

####Example Code of Movement Restriction

local ContextActionService = game:GetService("ContextActionService") # The ContextActionService
local FREEZE_ACTION = "freezeMovement" #The movement action
local uis = game:GetService("UserInputService") # To detect key press
qed = false # Toggler, to toggle between two states.

uis.InputBegan:Connect(function(obj, gp) # Function that is initiated every time the user presses a key.
	if obj.KeyCode == Enum.KeyCode.Q and not gp then # If the user happens to press the key 'Q' on their keyboard and if it wasn't while they were typing, then it goes on.
		if not qed then # Checks the state.
			ContextActionService:BindAction( # Binds the freeze action to the character.
    			FREEZE_ACTION,
    			function() 
					return Enum.ContextActionResult.Sink
				end,
    			false,
    			unpack(Enum.PlayerActions:GetEnumItems())
			)
		else
			ContextActionService:UnbindAction(FREEZE_ACTION) # Unbinds the freeze action from the character
		end
		qed = not qed # Sets the state to the opposite.
	end
end)

###This will basically toggle between the character movements being enabled or disabled every time they press 'Q' on the keyboard. Also, I was able to find out how to restrict movement using ContextAtionService through the following sites:

Why you need to return sink in the callback function.

How Bind Action works

The actual question that led to the answer.

##I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

KingLoneCat

This discussion is preserved as it was. New answers, comments, and votes are closed.