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

Why is the enter key not printing that it is being pressed?

Asked by 5 years ago

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)
0
try using keyboard input instead of the mouse method Donut792 216 — 5y

2 answers

Log in to vote
0
Answered by
Donut792 216 Moderation Voter
5 years ago
Edited 5 years ago
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

0
Dang I was to busy typing out my answer in a nice format you have ninja'd me! EpicMetatableMoment 1444 — 5y
0
Also I think you can just check if your variable `received` is true and just return to exit the function for handling text box focusing. EpicMetatableMoment 1444 — 5y
0
ah alright and lol i ninja'd you Donut792 216 — 5y
Ad
Log in to vote
2
Answered by 5 years ago

Question

Why is the enter key not printing that it is being pressed?

Answer

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

How to fix this?

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.

Example Code

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)

Answer this question