Help on InputBegan

I know I’ve been making a lot of posts lately, but i was told if i wanted to ask something i should ask.

Anyway i was trying to learn InputBegan as i’m currently learning how to script and make GUIs.

My understanding is that when the player is hovering over the GUI or presses a key that it will fire the event.

I’m looking for some clarification on this and examples if possible

Example :

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.F then -- Enum.KeyCode.F => Replace F With Your Key
      print("You pressed F")
    end
end)

and you can visit [ Roblox Developer API - InputBegan ] and check the Properties, Functions, …

InputBegan is when

  • A key is pressed
  • Mouse button is pressed
  • Touch on mobile devices

I believe those are all but may be wrong

Keep in mind that they will also fire if it’s processed by the game (ex: chat), a workaround is really simple

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(funciton(input,gameProcessed)
   if not gameProcessed then
      -- it was not processed by the game meaning it's not chat or a textbox for example
      -- input is an input object, read more about it here https://developer.roblox.com/en-us/api-reference/class/InputObject
   end
end)
1 Like

Thanks!! This really helps. Honestly before i got member on this forum i was learning .slowly and now i learn something new every day!:grinning:

1 Like