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

How can I bind a keyboard key to a function?

Hello helpers, I want to bind the key Left Ctrl to a function which reduces the players walkspeed.

Function =

local function ReduceSpeed()
	Player.WalkSpeed = 9
end

the Player variable is game.Players.LocalPlayer.Character.Humanoid. I've looked over the Roblox Wiki about ContextActionService, but I do not understand it at all. Someone please help! Thanks!

1 answer

You can use the InputBegan event of UserInputService to detect whenever any key is pressed, and then check if the key is left control, like so:

game:GetService("UserInputService").InputBegan:Connect(function(iobj, gp)
	if gp then --this variable is true if they are i.e. typing the text into a textbox
		return --so we abort if this is the case
	end
	if iobj.KeyCode == Enum.KeyCode.LeftControl then
		ReduceSpeed()
	end
end)

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