Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trying to find the username of a player when a few letters are add to a textbox not working?

Introduction

Hi I'm Matt, and I'm making my own custom admin panel. I'm having trouble with my account age finder script that finds a possible matching username of a player then sets a textbox text to the players username, then sets a textlabel to the players account age (Days old). There are no errors in the client or server, I don't know what I did wrong.

Code

Here is my code:

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

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

        if match then
            table.insert(matches, name)
			name.AccountAge = answertext
        end
    end
    
    if not #matches > 1 then
        buttontext = matches[1]
    end
end

Conclusion

In conclusion, I would like help with this, as I have looked over many dev forum posts, and the developer wiki and found nothing that could help. Any help is appreciated, this is my first time using strings!

Edits:

I might have found the problem, I forgot to connect the function. Still doesn't work, so if you find anything else please answer.

5 comments

0

what would you put for the text if you were an admin

0

Could you explain more? I'm confused.

0

like is it caps sensitive?

0

it should be the exact username the player has yes

0

I had a similar problem myself, I found a solution after a few days, it needs some explaining so if you still need help I'd rather you connect me in discord, [CLASSIFIED]#9999

1 answer

Alright so there are a few issues here. I'm going to assume this is all in some sort of screenGui only visible to the player. If it's visible to other players, it'll be a bit more complicated so let me know.

Firstly, this must be in a LocalScript. GUIs should generally be dealth with from the client, as it's the client who views them. In a LocalScript, you don't fire functions on player added, because LocalScripts only run at around the same time the player is added.

Instead, you want your function to run only when input has been entered into the TextBox. There's an event that can help us with this: FocusLost. This will make the function fire whenever new text is added and the player confirms. Your code should look roughly like this, in a LocalScript, located in the Gui:

local buttontext = script.Parent
local answertext = script.Parent.Parent.EnteredPlayerAccountAge

function findUser()
    local matches = {}

    for i, Player in ipairs(game.Players:GetPlayers()) do
        local name = Player.Name
        local match = string.find(name, buttontext.Text)

        if match then
            table.insert(matches, name)
            answertext.Text = tostring(Player.AccountAge)
        end
    end

    if not #matches > 1 then
        buttontext.Text = matches[1]
    end
end

script.Parent.FocusLost:Connect(findUser)

5 comments

0

Still doesn't work, yes I converted it to a LocalScript. No output, still doesn't print. I have a separate script that has PlayersAllowed which are the players that can see the gui.

0

I'll try this tomorrow. I'm not gonna watch Super Bowl 54

0

Try it again, just edited it a bit

0

So I tried it, the "for i, player in ipairs" prints! It doesnt work though, when i type a name in, the error "Players.matiss112233.PlayerGui.MattsAdminGui.MainFrame.RoundedBorders.CheckPlayerAccountAgeFrame.PlayerUsernameEnterBox.FindPlayersEnteredInBox:10: bad argument #2 to 'find' (string expected, got nil)"

0

That means you didn't write anything in the textBox

This discussion is preserved as it was. New answers, comments, and votes are closed.