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

How to Work wth Keyboard Input?

Hello, I am very confused, and someone with patience and kindness can explain to me somethings. I have made the following script:

function onKeyPress(key)
	if key == Enum.KeyCode.R then
		print("R was pressed")
	end
end
game.ContextActionService:BindAction(onKeyPress)

Can someone explain to me why in the wiki there are three parameters? Also, what is ContextActionService and BindAction? Thanks for reading, and again, sorry for my confusion. ~Yoman1776

1 answer

✓ Accepted answer

ContextActionService is a service that can be used to bind user input to functions.

Also, whoever gave you that script may have been wrong in that set up.

BindAction is a function used to bind a specific key (using it's keycode), to function with some additional options.

http://wiki.roblox.com/index.php?title=API:Class/ContextActionService http://wiki.roblox.com/index.php?title=ContextActionService_tutorial

This is how you use it:

local serv = game:GetService("ContextActionService")

function attack()
	print("he is attacking")
end

serv:BindAction("Attack",attack,false,Enum.KeyCode.One)

wait(1)

serv:UnbindAction("Attack")

These are the four parameters:

The name of the action, it has to be unique.

The function that gets called.

Whether or not a button for this action will be created on a mobile.

The key that gets pressed.

1 comment

0

Wow, thanks so much. That clarified absolutely everything! Thanks again!

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