Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

0

try using keyboard input instead of the mouse method

2 answers

✓ Accepted answer
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

3 comments

0

Dang I was to busy typing out my answer in a nice format you have ninja'd me!

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.

0

ah alright and lol i ninja'd you

##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)

This discussion is preserved as it was. New answers, comments, and votes are closed.