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.
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)