Autofill username script wont work with textbox

I have this script where it will autofill the textbox with the username if I look at part of the username in-game. Heres the problem tho,

  1. It won’t give any errors or output in the output

  2. It won’t put the username in the textbox.

  3. I tried looking for a fix but I can’t find anything!

Heres the script!:

function findUser()
local players = game.Players:GetPlayers()
local text = script.Parent.Text
local matches = {}

for i, Player in ipairs(players) do
    local name = Player.Name
    local term = string.sub(name, 1, string.len(text))
    
    local match = string.find(term, text)

    if match then
        table.insert(matches, name)
    end
end

-- If we don’t have multiple matches , do stuff
if not #matches > 1 then
    script.Parent.Text = matches[1]
end

end

Please fix it!

You don’t seem to be running your findUser() function?

Just wanted to mention on top of this:

If you only use the first match, you can put a break after your table.insert, meaning that code that doesn’t need to run, won’t run.

E.g.

if match then
    table.insert(matches, name)
    break -- the loop won't look for further records (this time only).
    -- you can learn more about 'break' on a lua manual.
end

Thanks let me try this! Hope this works

The script still did not work.

function findUser()
local players = game.Players:GetPlayers()
local text = script.Parent.Text
local matches = {}

for i, Player in ipairs(players) do
    local name = Player.Name
    local term = string.sub(name, 1, string.len(text))
    
    local match = string.find(term, text)

    if match then
table.insert(matches, name)

script.Parent.Text = matches
break – the loop won’t look for further records (this time only).
– you can learn more about ‘break’ on a lua manual.
end
end

-- If we don’t have multiple matches , do stuff
if not #matches > 1 then
    script.Parent.Text = matches[1]
end

end

For some reason it wont load or something

When I’m ever in doubt of a new chunk of code or it doesn’t seem to work I just casually throw a few print()'s in there after key parts(if statements and such) and check the output to see what has run. That could help you see where you are at. Good luck!

I tried that… Seems that the function is not firing…

As @coexperience mentioned above, have you called the function findUser() ? (It does not show in the post.) It may even be something simple. Example from experience: I might think something is wrong with my code and after a few minutes of searching for the bug I find that I disabled my script… Has happened a time or two. :stuck_out_tongue_winking_eye: