Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to Work wth Keyboard Input?

Asked by 9 years ago

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

Log in to vote
1
Answered by 9 years ago

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:

1)

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

2)

The function that gets called.

3)

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

4)

The key that gets pressed.

0
Wow, thanks so much. That clarified absolutely everything! Thanks again! yoman1776 85 — 9y
Ad

Answer this question