Can someone please fix this script or if you have time give me the non-deprecated version? I press the enter key but the script doesn't give any output.
mouse = game:GetService("Players").LocalPlayer:GetMouse() mouse.KeyDown:Connect(function(key) if key == Enum.KeyCode.Return then print("enter") end end)
local Service = game:GetService("UserInputService") -- grab the service Service.InputBegan:Connect(function(input, recieved) if input.UserInputType == Enum.UserInputType.Keyboard then -- checks if input is keyboard but is not nessesary if Service:GetFocusedTextBox() == nil then -- checks to see if a player is typing in chat or a gui and if not runs function but not nessesary if input.KeyCode == Enum.KeyCode.Return then -- the key print("enter") -- print end end end end)
this should work out for you
Why is the enter key not printing that it is being pressed?
Because the .KeyDown
signal of the client's mouse returns a string of what key was pressed not an enum.
By the way Donut792 provided some pretty useful insight in the comments so do read their comments since they will help you.
Donut has ninja'd me but I will still post xd
Use UserInputService as it is better to use the newer methods that are updated more frequently.
UserInputService has InputBegan
signal which passes an InputObject
for the first argument and the second argument was whether the player was typing or was interacting with some core gui stuff.
local userInputService = game:GetService("UserInputService") local function handleInputBegan(input, gpe) if gpe then return end --// stops code if they are interacting with core gui or typing if input.KeyCode == Enum.KeyCode.Return then print("Enter was pressed") end end userInputService.InputBegan:Connect(handleInputBegan)