[help] detect return input within textbox

heya! I’m trying to create a custom text that is on the side of the screen and I don’t quite know how to script it so that it checks if he is focused and presses enter rather than pressing enter/return unfocused.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local chatInput = script.Parent.ChatInput
local chatRemoteEvent = ReplicatedStorage:WaitForChild("Events_"):WaitForChild("ChatRemoteEvent")


local function onEnterPressed(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.Return and not gameProcessedEvent then
		if chatInput.Text ~= "" then
			chatRemoteEvent:FireServer(chatInput.Text)
			chatInput.Text = ""
		end
	end
end



chatInput.Focused:Connect(function()

	game:GetService("UserInputService").InputBegan:Connect(onEnterPressed)

end)

Inside the onEnterPressed function, you can check if textbox:IsFocused(), and if true, do the magic.

Example:

if textBox:IsFocused() then 
     if textBox.Text ~= "" then
		textBox.Text = ""
	end
end

and at the end of the script, you can just use the InputBegan function.

 game:GetService("UserInputService").InputBegan:Connect(onEnterPressed)