Getting CursorPosition/SelectionStart after pressing TextButton?

I need the CursorPosition and SelectionStart properties of a TextBox after pressing a button, however if a button is pressed those properties are reset to -1, so how can I get those before they’re reset?

Current code:

local Frame = script.Parent
local Button = Frame.Button
local InputBox = Frame.InputBox
local Result = Frame.TextLabel

Button.MouseButton1Click:Connect(function()
	local CursorPos = InputBox.CursorPosition
	local SelectionStart = InputBox.SelectionStart
	
	Result.Text = string.format("%s, %s", CursorPos, SelectionStart)
end)
1 Like

Consider using ROBLOX’s UserInputService on the UI Object you have there. You’ll be able to retrieve coordinates of your mouse as a parameter with Input.Position

Useful resources:

  1. GuiObject | Documentation - Roblox Creator Hub
  2. GuiObject | Documentation - Roblox Creator Hub

In your case:

YourGUIObject.InputBegan:Connect(function(Input)
  if Input.UserInputType == Enum.UserInputType.MouseButton1 then
    print(Input.Position .. " - your mouse position when the UI was clicked")
  end
end)

You may also mimic the SelectionStart property by checking for input changes.

I don’t think you understood the question, I’m trying to get the selected parts of a TextBox, you know how you can click and drag over certain text and its highlighted in blue? I don’t want the mouse position

I got over this in the past by using GuiObject.InputBegan to check for MouseMovement for a custom MouseEnter event, using that whenever a mouse is hovered over the button, I assign the SelectionStart and CursorPosition to variables, when the button is pressed, I use information from the variables.

This way you’ll have saved the required properties for use, regardless of the fact that the properties reset when clicking outside of the TextBox.