YouTube2Text

UserInputService - Roblox Advanced Scripting #13 — Transcript

by BrawlDev · 4,821 words · 640 segments · language en · Watch on YouTube

Full transcript

  1. 0:00press R I Dare You Well when you phrase
  2. 0:03it like that that sounds like a
  3. 0:05challenge so that's a challenge I can't
  4. 0:07back down from whoa whoa whoa what is
  5. 0:11happening right now what is happening
  6. 0:12with this game bro oh my God I need to
  7. 0:14get out of here this is too
  8. 0:16insane all right so that just happened
  9. 0:19um yeah the purpose of that whatever
  10. 0:22that thing was with the sky of whatever
  11. 0:24that was doing um that was supposed to
  12. 0:26represent user input service I guess uh
  13. 0:29so it's basically how we're able to
  14. 0:31detect user input from the player
  15. 0:33whenever we're making our Roblox games
  16. 0:35so an input could be if I'm clicking my
  17. 0:38mouse in the game uh if I'm pressing W
  18. 0:42to move forward if I'm just pressing any
  19. 0:45sort of keys that are on my keyboard
  20. 0:46right now uh you can actually see that
  21. 0:48in the output down here it's detecting
  22. 0:51every time I do any sort of input that
  23. 0:53is just going to print uh the string
  24. 0:55input that's because we're able to
  25. 0:57detect when the player is pressing down
  26. 0:59on a key uh on whatever device that
  27. 1:02they're playing on so for me I'm playing
  28. 1:03on a computer but let's say I was
  29. 1:05playing on a mobile device then we would
  30. 1:08probably detect when the player is
  31. 1:10moving or pressing the jump button uh or
  32. 1:13just pressing buttons on an Xbox
  33. 1:15controller or a PS4 controller now that
  34. 1:17Roblox is on PS4 so things like that and
  35. 1:19I want to be able to introduce this
  36. 1:21concept in Roblox studi so that you will
  37. 1:23know how to detect user input and also
  38. 1:27creating the best experience for players
  39. 1:29depending on the device that they are
  40. 1:30playing on so if that interests you then
  41. 1:34you should stick around till the end of
  42. 1:35this video so with that being said let's
  43. 1:37go straight into implementing all right
  44. 1:38let's think about this for a minute you
  45. 1:40can see that with user input service the
  46. 1:42thing we're going to be talking about in
  47. 1:43this episode there's so much to uncover
  48. 1:46with this concept that I'm going to be
  49. 1:49here forever if I'm going to try and go
  50. 1:51through every single thing that's inside
  51. 1:52of this documentation so what I'm going
  52. 1:54to do in this episode is give you a
  53. 1:56general introduction to how you can use
  54. 1:58user input server to get user input and
  55. 2:02also configure your game depending on
  56. 2:04what device the user is playing on I'm
  57. 2:06going to treat this episode as an
  58. 2:08introduction more than an in-depth guide
  59. 2:11compared to some of my other episodes
  60. 2:13and maybe in the future I will dive
  61. 2:15deeper into some of these Concepts um in
  62. 2:18specific videos but for the purposes of
  63. 2:20this episode I'm just going to go over
  64. 2:21some basic stuff for user input service
  65. 2:24and hopefully you will be able to
  66. 2:26experiment with user input service on
  67. 2:28your own time or stick with me for
  68. 2:30future videos where I talk more deeply
  69. 2:32into some of these concepts with user
  70. 2:34input service so with that being said
  71. 2:36let's now go and start implementing this
  72. 2:38in Roblox studio now that we understand
  73. 2:40it let's add user input into our game
  74. 2:42there's two important use cases to use
  75. 2:44user input service at least for the sake
  76. 2:46of this video where the first one is
  77. 2:49detecting user inputs so if they're
  78. 2:51pressing down a button uh while they're
  79. 2:53playing the game uh and just things like
  80. 2:55that the Second Use case is detecting
  81. 2:58what device the player is playing on so
  82. 2:59that we can give them the best
  83. 3:01experience depending on what they're
  84. 3:02playing on cuz obviously when a player
  85. 3:04is playing on a mobile device the
  86. 3:06experience is a lot different than
  87. 3:07playing on a PC so that's something
  88. 3:10we're going to have to detect in our
  89. 3:11game using user input service and before
  90. 3:13we start adding it you might ask does
  91. 3:15this belong in a script or a local
  92. 3:17script so user input service is only
  93. 3:21local sided this cannot be done on the
  94. 3:23server this can only be done from the
  95. 3:25player directly so you're going to have
  96. 3:27to put this in a local script probably
  97. 3:30somewhere that fits it best like starter
  98. 3:32character scripts uh so let's hit the
  99. 3:35plus sign next to starter character
  100. 3:36scripts and let's insert a local script
  101. 3:39so let's delete this and first thing we
  102. 3:41will do is make reference to user input
  103. 3:43service we're going to say local user
  104. 3:46input service equals game colon get
  105. 3:51service user input service so the first
  106. 3:55part of this video is just detecting
  107. 3:57user input whenever they press down on
  108. 3:59any any key this is going to use a
  109. 4:01function called input began and this is
  110. 4:03pretty much what it's going to look like
  111. 4:05so we're going to say user input service
  112. 4:09do input began colon connect function
  113. 4:15open and close parentheses enter so this
  114. 4:18input began event takes in two function
  115. 4:21parameters the first one is the user
  116. 4:23input and then the other one is a
  117. 4:26Boolean that's called game processed
  118. 4:29event I'm going to go over what this one
  119. 4:30does in a little bit but for now we're
  120. 4:32going to focus on the input which is
  121. 4:34basically an input object made by Roblox
  122. 4:37themselves that contains information
  123. 4:39that tell us uh what type of input the
  124. 4:42user is currently pressing down on when
  125. 4:45this event fires so if we were to not
  126. 4:47pay attention to the input and just
  127. 4:49simply uh do something whenever the user
  128. 4:52does anything related to user input
  129. 4:54let's just make a print statement really
  130. 4:56quickly just to see whether this is
  131. 4:57working or not so we'll just simply say
  132. 4:59input and then let's see what this looks
  133. 5:01like when we play the game so in the
  134. 5:03game I can literally do anything to make
  135. 5:05it so that the input began event will
  136. 5:07fire I can rightclick and it will say
  137. 5:11input has been printed down here I can
  138. 5:13move around and it'll constantly print
  139. 5:15out input because I'm pressing down on
  140. 5:17multiple keys while I'm moving my
  141. 5:20character if I jump or just like really
  142. 5:23press anything on my keyboard then it's
  143. 5:25going to activate the input began uh
  144. 5:28event and this basically applies with
  145. 5:31any input that I do so not only is this
  146. 5:33going to work with keyboard and mouse
  147. 5:35but this would also apply to if you're
  148. 5:36playing on a phone a tablet or even on
  149. 5:40an Xbox or PS4 just any sort of input
  150. 5:44will be detected using the input began
  151. 5:46but this is important to note when we're
  152. 5:48pressing down on a key and not when
  153. 5:50we're lifting it back up I'm going to
  154. 5:51show you how we're going to detect that
  155. 5:53in a little bit so let's go back to our
  156. 5:54script and we can see that this is
  157. 5:56basically being printed anytime we press
  158. 5:59any input so it doesn't really matter
  159. 6:01what input it is as long as we're
  160. 6:03pressing down something but now let's
  161. 6:05say we wanted to focus on specifically
  162. 6:07which input we want to uh look out for
  163. 6:10whenever we're pressing down a key so
  164. 6:12let's say we want the player to press a
  165. 6:14specific key then we want it to do
  166. 6:16something uh once we know that we press
  167. 6:18something so you know with my uh very
  168. 6:21weird example from the beginning where I
  169. 6:23pressed R and that specifically
  170. 6:25triggered something uh we can do that
  171. 6:27using this input that was passed in to
  172. 6:30check whether this input was
  173. 6:31specifically R so we're going to do that
  174. 6:33down here by dropping two lines and
  175. 6:36we're just going to do a simple if
  176. 6:37statement we're just going to say if
  177. 6:39input now the thing about input is that
  178. 6:42this is an input object and it has
  179. 6:44multiple properties that are layered
  180. 6:46onto this input object the thing we're
  181. 6:49most interested in is the key that was
  182. 6:52pressed so this property is called key
  183. 6:56code which is an enum value uh which
  184. 6:59describes the kind of input that is
  185. 7:01being pressed from the user so this is
  186. 7:04how we're going to detect whether the
  187. 7:06input is the one we're looking for it's
  188. 7:08not just input because input is an
  189. 7:10object we're looking for the key code
  190. 7:12property so then we're going to say if
  191. 7:15this is equal to uh R so we can do it
  192. 7:18like this as a as a character but the
  193. 7:20more conventional way of doing this is
  194. 7:22by using an enum value from key code so
  195. 7:25you know how key code is an enum value
  196. 7:27we want this to match a key code enum
  197. 7:30that exists in roblox's library already
  198. 7:33so we're going to do that by saying enum
  199. 7:36dokey code Dot and as you can see
  200. 7:40there's a whole list of options that we
  201. 7:42can choose from using this key code enum
  202. 7:45now keycode is a specific thing for
  203. 7:47certain keyboards so this will not work
  204. 7:49for devices such as uh phones tablets
  205. 7:53and consoles this is specifically for
  206. 7:55keyboards uh so let's say we want it to
  207. 7:58be r then we're going to say uh print
  208. 8:02player has pressed R so this is the
  209. 8:06syntax we're going to use to figure out
  210. 8:08whether the player has pressed down the
  211. 8:10r key using the key code enum from our
  212. 8:14user input so let's go into the game and
  213. 8:16try this out all right so over here I'm
  214. 8:18just spamming uh my keyboard to print
  215. 8:21input but I'm not pressing R yet uh now
  216. 8:25when I do press R you can see that in
  217. 8:27the output it says player has pressed PR
  218. 8:29R because we made a check to see if the
  219. 8:31key code is equal to R specifically then
  220. 8:34we're going to execute whatever is down
  221. 8:36here and not just any random input that
  222. 8:39um we set out input began to detect so
  223. 8:43that pretty much works as expected so I
  224. 8:45mentioned that input began has two
  225. 8:47arguments so we have the input and then
  226. 8:49we have this random Boolean called game
  227. 8:51processed event what is this so let's
  228. 8:54first write down what actually happens
  229. 8:56uh when we do check whether this game
  230. 8:58process proed event is true or not so
  231. 9:00let's just simply write if game
  232. 9:03processed event then print game
  233. 9:07processed event now let's go into the
  234. 9:10game so I can demonstrate to you what
  235. 9:11this does all right so I'm in the game
  236. 9:13let's say for instance I want to type
  237. 9:15something in the chat for whatever
  238. 9:16reason so I'm going to press the chat
  239. 9:18and then I'm just going to type in uh
  240. 9:20Rat I don't know just like something
  241. 9:22completely random obviously if I have a
  242. 9:24local script that's detecting user input
  243. 9:27I needed to press down the r a key to be
  244. 9:30able to press the to be able to type out
  245. 9:32this message but what if I don't want to
  246. 9:35detect this uh user input while the
  247. 9:38while the user is just typing something
  248. 9:40in the chat that's basically what game
  249. 9:42processed event is we're seeing whether
  250. 9:44the player is currently interacting with
  251. 9:47roblox's GUI system or I believe any GUI
  252. 9:51in general as a matter of fact I could
  253. 9:52be wrong about that with certain
  254. 9:54interactions like this especially with
  255. 9:56the chat we don't all of a sudden want
  256. 9:58it to do what it needs to do with this
  257. 10:00script if let's say the player is
  258. 10:02chatting something using Roblox GUI
  259. 10:05that's where we detect with game
  260. 10:06processed event if the player is
  261. 10:08currently interacting with roblox's GUI
  262. 10:10system then we don't want to do the user
  263. 10:12input checking so that's why in most
  264. 10:14cases we would just simply return this
  265. 10:17input began event because if let's say
  266. 10:19the user is typing something in the chat
  267. 10:21we don't want to process uh this input
  268. 10:24began event we just want to Simply
  269. 10:26return this uh with no issues whatsoever
  270. 10:28and that's basically what game processed
  271. 10:30event is I hope that made sense to you
  272. 10:32all right so we have an event for input
  273. 10:34began which is basically when the player
  274. 10:36presses down on a key but now let's try
  275. 10:39making another event to detect when the
  276. 10:41player lifts the key back up uh we will
  277. 10:44do that using an event called input
  278. 10:46ended so down here the syntax is
  279. 10:48relatively similarish so we're going to
  280. 10:51say user input
  281. 10:53service. input ended colon connect
  282. 10:57function open parentheses close
  283. 10:59parentheses input and then game
  284. 11:02processed event and this will pretty
  285. 11:04much fire whenever the player lifts a
  286. 11:07key off of the key or any input in
  287. 11:09general so we're going to say user Has
  288. 11:12Lifted the input and so we can go into
  289. 11:15the game and see this change anytime I
  290. 11:18press down a key and then lift it back
  291. 11:19up so I'm going to hold the left Mouse
  292. 11:21button so I'm going to press the left
  293. 11:23Mouse button it still says the input is
  294. 11:26down there but then when I Let Go it
  295. 11:28says you user Has Lifted the input so
  296. 11:30this pretty much happens uh whenever we
  297. 11:33just let go of any sort of keys so I'm
  298. 11:35currently pressing s if I let go then
  299. 11:37it's going to say user Has Lifted the
  300. 11:38input and this can be done for every
  301. 11:41single input input began and input ended
  302. 11:44so that's one quick little thing I
  303. 11:45wanted to introduce there uh to get you
  304. 11:47familiar with that so over here we
  305. 11:48detected user input based on the key
  306. 11:51that they were pressing on the keyboard
  307. 11:52but obviously there's way more different
  308. 11:54types of input other than just keys on
  309. 11:56the keyboard like my mouse or or a
  310. 11:59Gamepad or mobile things like that uh we
  311. 12:02can do that using another property of
  312. 12:05input not called key code but called
  313. 12:08user input type and this is what it's
  314. 12:10going to look like so let's detect if
  315. 12:12the player is pressing the left Mouse
  316. 12:14button uh we're going to do that by
  317. 12:16typing if input.
  318. 12:19userinput type equals and this is going
  319. 12:22to be another enum value so we're going
  320. 12:24to say enum do userinput type Dot and
  321. 12:29there's a whole bunch of options here so
  322. 12:31Mouse button one is basically our left
  323. 12:33Mouse button and then there's Mouse
  324. 12:35button two which is our right Mouse
  325. 12:37button and then Mouse button 3 which is
  326. 12:39like the little mouse scroll wheel if we
  327. 12:41press that down and then there's a bunch
  328. 12:43of other stuff too that we're not really
  329. 12:44going to uh worry too much about as of
  330. 12:47right now so we're going to say mouse
  331. 12:49button one then print user is pressing
  332. 12:56the left Mouse button and so if we try
  333. 12:59this out in game this is how we're going
  334. 13:00to detect Mouse Clicks in our in our
  335. 13:03game so I'm going to hit the left Mouse
  336. 13:04button and it's going to say user is
  337. 13:06pressing the left Mouse button and this
  338. 13:08will only work with the left one if I
  339. 13:10try pressing the right one it's not
  340. 13:11going to print the left Mouse button or
  341. 13:14if I even press the middle button it's
  342. 13:15not going to say left Mouse button Mouse
  343. 13:18button one is specifically the left uh
  344. 13:21Mouse button and so that's pretty much
  345. 13:23how that works we can do the same thing
  346. 13:25with mouse button 2 Mouse button 3 and
  347. 13:27we can also do this with with different
  348. 13:29device inputs as well we can say touch
  349. 13:33which basically means that the user is
  350. 13:35on a touch device so like a mobile
  351. 13:38device like an iPhone or a tablet this
  352. 13:41is how we can detect that using this and
  353. 13:43even a gamepad as well so usually it
  354. 13:45would be Gamepad 1 if we're just
  355. 13:47detecting player one on like an Xbox or
  356. 13:49a PS4 this is how we would detect that
  357. 13:52it's pretty much one way of detecting
  358. 13:54the type of user input just by using
  359. 13:56user input type when we're checking the
  360. 13:58this in our input began event so this
  361. 14:01was one way of detecting the user input
  362. 14:03type of whether the user is playing on a
  363. 14:05phone a tablet a console things like
  364. 14:08that but there's actually an easier way
  365. 14:10to do this without doing this if
  366. 14:11statement check inside of this input
  367. 14:13began uh event because user input
  368. 14:16service has these properties that
  369. 14:18automatically tell us whether the user
  370. 14:20is playing on a specific device so these
  371. 14:23properties are usually named after the
  372. 14:25device that they're playing on so touch
  373. 14:27enabled is telling us if the player is
  374. 14:29on a mobile device gam pad enabled is if
  375. 14:32we're if we're playing on a console and
  376. 14:33things like that it's pretty simple
  377. 14:35actually so let's drop two lines up here
  378. 14:38to make a check whenever the player
  379. 14:40joins into the game to see what device
  380. 14:42they're playing on we're just simply
  381. 14:43going to say if user input
  382. 14:47service. touch enabled so this one tells
  383. 14:51us if the user's device has a touch
  384. 14:54screen so this is usually for mobile
  385. 14:56phones uh we're going to then say uh
  386. 14:59print player is playing on a mobile
  387. 15:03device and uh we can do something cool
  388. 15:05with this as well to really show us the
  389. 15:07difference so we're going to say
  390. 15:09workspace colon wait for child base
  391. 15:13plate and we're going to put in a wait
  392. 15:14for child so that it can load in fast
  393. 15:16enough color equals color 3. from RGB
  394. 15:21we're probably going to do some blue
  395. 15:23color hit okay and then let's go into
  396. 15:25the game and try this out all right so
  397. 15:27we're in the game and as you can see the
  398. 15:28base Plate's color has changed to blue
  399. 15:31because we are playing on a phone if I
  400. 15:33were to back out of this and then get
  401. 15:35off of device emulation play on my PC
  402. 15:38then this change should not happen uh
  403. 15:41since I'm now playing on a PC so yeah
  404. 15:43because now that I'm playing on a PC the
  405. 15:45script is not going to tell me that uh
  406. 15:47I'm playing on a mobile device in order
  407. 15:49to change this base Plate's colors so we
  408. 15:51were able to do that successfully and we
  409. 15:53can do this with console as well just by
  410. 15:57simply replacing touch enabled with
  411. 16:00gamad enabled and uh we can also do this
  412. 16:03with VR as well if you're playing Roblox
  413. 16:05VR just by saying VR enabled so these
  414. 16:10properties can pretty much signify
  415. 16:12whether the player is playing on a
  416. 16:14specific kind of device and you can use
  417. 16:16this property from user input service to
  418. 16:18do specific things with the player
  419. 16:21experience like changing guis to fit the
  420. 16:25player screen better uh or modify GUI
  421. 16:28screen or modify user controls based on
  422. 16:30the device that they're playing on and
  423. 16:32another instance I can think of that
  424. 16:33this would be very useful for is if
  425. 16:35you're using tools where the tool will
  426. 16:37play out differently depending on the
  427. 16:39device that you're playing on because a
  428. 16:40lot of the times tools require different
  429. 16:42controls for different devices that the
  430. 16:45user is playing on you're not going to
  431. 16:46be able to press W as and D if you're on
  432. 16:48a mobile phone and this is especially
  433. 16:50true for tools if you're trying to press
  434. 16:52specific keys that you're not able to on
  435. 16:54a different device so that's something
  436. 16:56else I wanted to throw in there user
  437. 16:58input service also allows us to detect
  438. 17:00when the player is jumping uh using an
  439. 17:03event called jump request so this is
  440. 17:06what it's going to look like if we just
  441. 17:07go down here and then uh say
  442. 17:10userinput
  443. 17:12service. JUMP request and this is
  444. 17:16basically going to fire every single
  445. 17:18time the player wants to make a request
  446. 17:20that they're jumping also known as
  447. 17:21pressing the space bar uh or pressing
  448. 17:24the jump button or a button whatever
  449. 17:26device that they're playing on just
  450. 17:27making any requests that makes the
  451. 17:30player jump we're going to say
  452. 17:32connect function open parentheses close
  453. 17:34parentheses this one has no arguments so
  454. 17:36we can just hit enter and now we could
  455. 17:37just print something like player has
  456. 17:40jumped you might think that this is
  457. 17:42basically the only thing we need to do
  458. 17:44but that's actually not correct because
  459. 17:46let's say we do try playing the game and
  460. 17:48try to make a jump request uh let's see
  461. 17:50what happens in the output when we do
  462. 17:52try and jump so I'm going to hit space
  463. 17:54bar and down here it said the player has
  464. 17:57jumped but I only pressed press spacebar
  465. 17:59one time but for some reason this
  466. 18:01printed three times and if I try this
  467. 18:03again I press spacebar it's continuously
  468. 18:06printing three or four different times
  469. 18:08maybe even more every time I just press
  470. 18:11the space bar button this especially
  471. 18:13happens when I'm holding down the space
  472. 18:14bar when it's just printing like this
  473. 18:16many times the reason it's doing that is
  474. 18:18it's because the event is detecting when
  475. 18:20a jump request is happening and a jump
  476. 18:22request can pretty much happen every
  477. 18:24millisecond that we try and attempt this
  478. 18:27jump Quest so an easy solution to this
  479. 18:30is to wrap this in a debounce Boolean
  480. 18:32variable uh so up here we could just
  481. 18:35write a very quick uh Boolean by saying
  482. 18:38local is jumping equals false uh and
  483. 18:42then down here we can make a check by
  484. 18:44saying if not is
  485. 18:47jumping then we will set is jumping
  486. 18:50equal to true and then we're going to
  487. 18:52take our print statement and then put it
  488. 18:53in here uh and then what we'll do is put
  489. 18:56a task. weight statement over here like
  490. 18:59maybe about 0.5 seconds and then set is
  491. 19:02jumping back to false so when the player
  492. 19:05does decide to jump then we will make
  493. 19:07the script wait for a little bit before
  494. 19:10setting is jumping back to false so that
  495. 19:12we can make the jump request check again
  496. 19:14and this should pretty much prevent our
  497. 19:16issue that we had before all right so
  498. 19:18I'm going to hit jump and it says the
  499. 19:20player has jumped so if I keep holding
  500. 19:22it then it's going to print player has
  501. 19:25jumped whenever my character gets back
  502. 19:28down on the ground to then print it
  503. 19:30again in the output so it pretty much
  504. 19:33now works as expected this is very
  505. 19:34useful if we're trying to detect when
  506. 19:36the player is jumping of course and that
  507. 19:38is pretty much the end of that what if
  508. 19:39we wanted to detect multiple key presses
  509. 19:42there's actually a couple ways we can do
  510. 19:43this but the easiest way I know is by
  511. 19:46doing it is by using a function inside
  512. 19:49of input began whenever we press uh a
  513. 19:52new key uh and this function is called
  514. 19:55is key down so we can use this function
  515. 19:58to tell whether this whether the player
  516. 20:00is currently holding down this key
  517. 20:02alongside this new key that they just
  518. 20:04pressed down uh so we can probably do
  519. 20:06that by modifying this if statement
  520. 20:07right here uh with our input begin so
  521. 20:10what if we wanted to hold the keys R and
  522. 20:13Q at the exact same time we can pretty
  523. 20:15much do this by first checking whether
  524. 20:17the player is already holding down the q
  525. 20:19key and then we can fire this down here
  526. 20:21when the new input the player is uh
  527. 20:24pressing down on is R right before this
  528. 20:26we're going to say if
  529. 20:29if user input service colon is key down
  530. 20:35and this takes in a uh key code uh
  531. 20:39parameter so we're going to say enum
  532. 20:41dokey code. q and then what we're going
  533. 20:45to do is put an and here so that we're
  534. 20:48going to say if the q key is currently
  535. 20:51being pressed down right now and then
  536. 20:53our new input is going to be uh R then
  537. 20:57we're going to print player has pressed
  538. 20:59R while holding q and so that's pretty
  539. 21:02much how we're going to detect whether
  540. 21:04multiple keys are being pressed at the
  541. 21:05same time so let's hit play and see what
  542. 21:08happens all right so I'm pressing R and
  543. 21:11as you can see nothing's being printed
  544. 21:12right now because I need to hold Q first
  545. 21:15so let's hold q and then let's press R
  546. 21:18so now it says player has pressed R
  547. 21:20while holding Q so that is pretty much
  548. 21:23how we detect multiple keys at the same
  549. 21:25time so now that we bring up the idea of
  550. 21:27pressing multiple keys what if we wanted
  551. 21:29to do a double tap on a key we'd pretty
  552. 21:32much do this using a feature called tick
  553. 21:34which I don't expect you to know about
  554. 21:35because we never talked about this yet
  555. 21:37uh but just for the sake of this uh
  556. 21:39example you can follow what I do but for
  557. 21:42a basic explanation tick is basically a
  558. 21:44feature that lets us keep track of time
  559. 21:46in our script what if let's say we
  560. 21:48wanted to detect whether a player is
  561. 21:50double tapping W uh to do some sort of
  562. 21:53cool functionality with it we can first
  563. 21:55do that by creating an if statement down
  564. 21:56here saying if input. keycode equals en.
  565. 22:03key key code. W then print player
  566. 22:10has double pressed W now obviously if we
  567. 22:14do this this is just one input of w not
  568. 22:17double pressing because we want to have
  569. 22:19the double press happen during a
  570. 22:21specific window of time so that it would
  571. 22:23count as a double press so double press
  572. 22:25would probably be like if uh if we press
  573. 22:28W once and then press W twice we want to
  574. 22:31have it in the span of like 0.5 seconds
  575. 22:33or something so that we can make it
  576. 22:34count as a double press and not just a
  577. 22:37single press uh so up here let's write a
  578. 22:41tick variable by saying local current
  579. 22:44tick equals tick open and close
  580. 22:47parentheses and then down here in our if
  581. 22:49statement let's say
  582. 22:51and current tick minus
  583. 22:56tick is less less than or equal to 0.2
  584. 23:00let's make it 0.2 in this case and let's
  585. 23:02also wrap this uh condition in a
  586. 23:05parenthesis so that we don't get
  587. 23:06confused uh with it and then down here
  588. 23:08let's write current tick equals tick
  589. 23:12open close parentheses so this is pretty
  590. 23:14much how we would be able to track uh in
  591. 23:17time uh whether the player is double
  592. 23:20pressing W or just pressing W singularly
  593. 23:22if it passes 0.2 seconds in between when
  594. 23:25we press W oops I made a little tyo here
  595. 23:27my bad uh so it's tick first and then
  596. 23:32minus current tick uh my apologies on
  597. 23:35that let's play the game all right so
  598. 23:36I'm pressing W and nothing is being
  599. 23:38printed if I press it again nothing is
  600. 23:41being printed if I double press it then
  601. 23:43it's going to say player has double
  602. 23:44pressed W because we very quickly press
  603. 23:47W within the span of under 0.2 seconds
  604. 23:51or exactly at 0.2 seconds so let's press
  605. 23:54it again nothing's being printed press
  606. 23:55it again nothing's being printed and
  607. 23:58it's still not printing so let's rapid
  608. 24:01tap it and now it says player has double
  609. 24:03pressed W so it's very quick and
  610. 24:06obviously we can adjust um this duration
  611. 24:09by changing this number from 0.2 to
  612. 24:11maybe 0.5 to make it more lenient but
  613. 24:13you get the general idea I don't expect
  614. 24:16you to know about tick until we discuss
  615. 24:18about it in a whole another video but
  616. 24:20this is one way of detecting whether a
  617. 24:23player is double tapping on a key I
  618. 24:26think that'll pretty much wrap up the
  619. 24:27introduction to user input service it's
  620. 24:29a very big feature that Roblox has and I
  621. 24:32encourage you to go and try some of
  622. 24:34these other functions and events and uh
  623. 24:37all sorts of stuff on the API that
  624. 24:38Roblox has provided for you it's a very
  625. 24:40big concept to know about because user
  626. 24:43input is one of the most fundamental
  627. 24:45things that that scripts can do when
  628. 24:48making the best experience possible in
  629. 24:50the games that you're playing if you've
  630. 24:51made it this far into the video then I
  631. 24:53encourage you to watch my other videos
  632. 24:54on my Advanced scripting tutorial
  633. 24:56playlist or just check out any of my
  634. 24:57other videos that I have on my channel
  635. 24:59but with that being said that's pretty
  636. 25:00much where I'm going to end it if you
  637. 25:02enjoyed this video then be sure to join
  638. 25:03my Discord server the link is in the
  639. 25:04description and I will see you in the
  640. 25:06next episode take care

About this transcript

This page contains the full transcript of UserInputService - Roblox Advanced Scripting #13 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 4,821 words across 640 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.