script.Parent.MouseButton1Click:Connect(function()
✓ ANSWER ACCEPTED
how can i change it to wehn u press a key on the keybord ?
3 answers
✓ Accepted answer
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.
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)
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)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
thhhxxx <3
xd still dont work