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

how can i change it to wehn u press a key on the keybord ?

Asked by
hacccke -3
3 years ago

script.Parent.MouseButton1Click:Connect(function()

3 answers

Log in to vote
2
Answered by
2_MMZ 1059 Moderation Voter
3 years ago
Edited 3 years ago

You need to use UserInputService. This is the easiest way to do so. (And probably the only way.) You'll need to do this in a LocalScript. This LocalScript can be ONLY inserted into StarterGui OR ServerScriptService. This can not be done with a regular/workspace script. Next, you'll type this following code:

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input,GPE)
      if input.KeyCode == Enum.KeyCode. then  --Put the desired button.
--Example: if input.KeyCode == Enum.KeyCode.E then
       --Your code goes here.
end
end)

I hope this helped you.

Ad
Log in to vote
0
Answered by 3 years ago
local userInputService = game:GetService("UserInputService") -- Basically can detect if any type of input has begun
local key = Enum.KeyCode.G -- put your key here
userInputService.InputBegan:Connect(function(input, isTyping)
if isTyping then return end 
if input.KeyCode == key then 
-- do what you want to run here
end
end)
0
thhhxxx <3 hacccke -3 — 3y
1
xd still dont work hacccke -3 — 3y
Log in to vote
0
Answered by 3 years ago

The answer MMZ gave works great and fine, but I'd really recommend using ContextActionService if it's a repeated action, such as reloading a gun or using a skill.

local contextActionService = game:GetService("ContextActionService")

local action = "Action"

local actionFunction(actionName, inputState, inputObject)
    if actionName == action and inputState == Enum.UserInputState.YourInputStateHere then
        -- Your code goes here
    end
end

contextActionService:BindAction(action, actionFunction, true, Enum.KeyCode.YourKeyHere)

Answer this question