Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Does "GetFocusedTextBox()" only work on textboxes named "TextBox"?

Asked by
nachsor 36
5 years ago

Hello i am currently working on a game and i had ran into a problem. I am generating a couple TextBoxes on another script and i wanted to do so that when you press "Return" key on the keyboard it would get you to the next textbox. All of my text boxes are generated with the name of "TextBox" and a number which goes from 0 to 209. My problem is that i am getting an error on this line:

local TextBoxFocused = UserInputService:GetFocusedTextBox()

The error says the following: attempt to index local 'TextBoxFocused' (a nil value)

If i understand correctly, it says that TextBoxFocused is nil. I tried with a text box called "TextBox" and it worked just fine but a text box with the number at the end gives me the error. Any clues why?

Script:

wait(0.02)
for i,v in pairs(script.Parent:GetChildren()) do

if string.match(v.Name, "TextBox") then

print(v.Name)

v.Text = ""

v.ClearTextOnFocus = false

end

end











local UserInputService = game:GetService("UserInputService")





UserInputService.InputBegan:connect(function(input)

if UserInputService:GetFocusedTextBox() ~= nil then

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

end

end)



function onKeyPress(inputObject, gameProcessedEvent)

if inputObject.KeyCode == Enum.KeyCode.Return then

local TextBoxFocused = UserInputService:GetFocusedTextBox()

local CharacterAfterO = string.find(TextBoxFocused.Name, "o")

print(string.sub(TextBoxFocused.Name, CharacterAfterO + 2))

print("HI")

end

end

Please Help, Thank you

Nachsor

0
No User#24403 69 — 5y
0
Yah, I wouldn't think that's a thing starmaq 1290 — 5y
0
And even if there was no textbox, and it was nil that shoul'nt error, it will just be set to nil. Are you sure the error is from that line? starmaq 1290 — 5y
0
Yes, I am fairly certain. This is the full error just incase it helps: 11:40:23.248 - Players.nachsor.PlayerGui.ScreenGui.ScrollingFrame.ChangeText:26: attempt to index local 'TextBoxFocused' (a nil value). When I click on the error, it gets me straight to line 26. This one, local CharacterAfterO = string.find(TextBoxFocused.Name, "o") nachsor 36 — 5y

1 answer

Log in to vote
0
Answered by
noammao 294 Moderation Voter
5 years ago

This is not really an answer with a lot of effort but I'm just going give a link to the roblox developer website. Maybe it will help a little. idk.

This function returns the [TextBox] the client is currently focused on. A TextBox can be manually selected by the user, or selection can be forced using the [TextBox:CaptureFocus](https://developer.roblox.com/api-reference/function/TextBox/CaptureFocus) function. If no TextBox is selected, this function will return nil.

As [UserInputService](https://developer.roblox.com/api-reference/class/UserInputService) is client-side only, this function can only be used in a [LocalScript](https://developer.roblox.com/api-reference/class/LocalScript).

Link: Developer.roblox. UserInputService:GetFocusedTextBox

0
I just realised how messy that looks. Also as I said this isn't really an answer so if you have already looked at the api or you don't find this helpful then ignore it. noammao 294 — 5y
Ad

Answer this question