0

An example would have been like

local E = game:GetService('UserInputService').SetKeyDown(Enum.KeyCode.E) 

but it doesnt work ofcourse because i cant jsut make my game press E by itself with this thing, so it requieres soemthing longer and if you find the solution can you also do one where it makes it pressed down?

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
demxkero
  • 26
  • 1
  • 3

2 Answers2

0

The input can only be registered on the client, therefore you will have to code in a LocalScript. There are 2 services which are used to get the player's input:-

This example shows how to use the UserInputService in getting the player's LeftMouseButton input.

local UserInputService = game:GetService("UserInputService")
 
local function onInputBegan(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        print("The left mouse button has been pressed!")
    end
end
 
UserInputService.InputBegan:Connect(onInputBegan)

This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon.

local ContextActionService = game:GetService("ContextActionService")
 
local ACTION_RELOAD = "Reload"
 
local tool = script.Parent
 
local function handleAction(actionName, inputState, inputObject)
    if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
        print("Reloading!")
    end
end
 
tool.Equipped:Connect(function ()
    ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)

You should take a look at the Wiki pages.

boru
  • 42
  • 6
0

It sounds like you want to write a script to press the E key, but that isn't possible.

You can bind actions to keypresses, just like in the examples Giant427 provided, and you can also manually call the functions that are bound to those actions, but you cannot write a script to fire keyboard inputs.

Kylaaa
  • 6,349
  • 2
  • 16
  • 27
  • It is possible if you use events – Glow Bunny Sep 17 '21 at 03:50
  • @GlowBunny, [ContextActionService.BindAction](https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction), [UserInputService.InputBegan](https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputBegan), [UserInputService.InputChanged](https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputChanged), and [UserInputService.InputEnded](https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputEnded) all observe the keyboard state. But you can't write a script to trigger those events manually. – Kylaaa Sep 17 '21 at 13:58
  • No events as in bindable events, just fire them when they need it and when it fires, run the same code as if you pressed e – Glow Bunny Sep 19 '21 at 12:19
  • You're describing a way that you might execute a function that is also bound to a keypress. The original question was asking how you would write a script to fire the keypressed event itself, presumably for something like an auto-clicker. And that is the piece I'm saying isn't possible. – Kylaaa Sep 19 '21 at 14:28