So, I want to learn how to execute a chunk of code when a player presses and holds a key but i'm not sure how to approach this, any help?
01 | player = game.Players.LocalPlayer |
02 | mouse = player:GetMouse() |
03 |
04 | mouse.KeyDown:connect( function (key) |
05 | print (key:byte()) |
06 | if key = = "f" then |
07 | print ( "wassup" ) |
08 | else |
09 | print ( "hi" ) |
10 | end |
11 | end ) |
All you have to do is use a while loop to keep track of the time the key has been pressed. You also have to detect when the key has been "un-pressed" to reset that time. Like this :
01 | player = game.Players.LocalPlayer |
02 | mouse = player:GetMouse() |
03 | fPressed = false |
04 | timePressed = 0 |
05 |
06 | mouse.KeyDown:connect( function (key) |
07 | if key = = "f" then |
08 | fPressed = true |
09 | end |
10 | end ) |
11 |
12 | mouse.KeyUp:connect( function (key) |
13 | if key = = "f" then |
14 | fPressed = false |
15 | timePressed = 0 |