Get the last thing entered in a TextBox

I want to get the last thing entered in a TextBox (e.g. a number, letter, emoji or etc). I tried using TextBox.Text:sub(TextBox.CursorPosition - 1, TextBox.CursorPosition) but it doesn’t work well with emojis.

Does anyone have a good solution?

Have you tried using string.sub on the textlbl’s .Text?

im guessing you do care that the user might add characters in the middle of the text so here is a script for that.

local textBox = script.Parent:WaitForChild("TextBox")

local previousText = ""

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	local newText = textBox.Text

	-- Find the difference
	local oldLength = #previousText
	local newLength = #newText

	local lastCharacterEntered

	if newLength > oldLength then
		-- Character(s) added
		for i = 1, newLength do
			if previousText:sub(i, i) ~= newText:sub(i, i) then
				lastCharacterEntered = newText:sub(i, i)
				break
			end
		end

		-- Fallback to last char if none detected
		lastCharacterEntered = lastCharacterEntered or newText:sub(-1)

		print("Last character entered:", lastCharacterEntered)
	end

	previousText = newText
end)

also it only shows the first character of your input, so if you paste text it will only show the first character of what you paste

local LastCharacter = string.sub(TextBox.Text, -1)