Show bubble when player is typing (not working)

Hi,

I’m trying to make it so that when the player is typing it will show a GUI above their head and as soon as the message is sent or completely removed it will hide the GUI.

it’s not working as intended because so far it only shows the GUI when typing, but does not hide again when I remove my typing or hit send.

StarterPlayerScript:

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local typingIndicatorTemplate = ReplicatedStorage:WaitForChild("TypingIndicator")
local player = Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")

local typingIndicator = typingIndicatorTemplate:Clone()
typingIndicator.Parent = head
typingIndicator.Adornee = head
typingIndicator.Enabled = false

local function animateDots()
	while typingIndicator.Enabled do
		local dot1 = typingIndicator:WaitForChild("Dot1")
		local dot2 = typingIndicator:WaitForChild("Dot2")
		local dot3 = typingIndicator:WaitForChild("Dot3")

		dot1.Visible = true
		wait(0.3)
		dot2.Visible = true
		wait(0.3)
		dot3.Visible = true
		wait(0.3)

		dot1.Visible = false
		dot2.Visible = false
		dot3.Visible = false
		wait(0.3)
	end
end

spawn(animateDots)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent and input.UserInputType == Enum.UserInputType.Keyboard then
		typingIndicator.Enabled = true
	end
end)

local function hideTypingIndicator()
	typingIndicator.Enabled = false
end

player.Chatted:Connect(hideTypingIndicator)
UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent and input.KeyCode == Enum.KeyCode.Return then
		hideTypingIndicator()
	end
end)

I’m not too sure because its around midnight for me, but arent you supposed to do “if not gameProcessedEvent” instead of “if gameProcessedEvent”?