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

How do you make a script that auto completes names?

Asked by 5 years ago

I'm trying to make a script where you can find someone's name by typing only the first few letters. Ex: A player's username is "testING123" and you type "testing" it will print "testING123" Can someone please help me with this?

1 answer

Log in to vote
0
Answered by
Vathriel 510 Moderation Voter
5 years ago
Edited 5 years ago

Well if you have a table of the players, you can use string.match to tell whether or not your search string is contained within any of the names.

Example:

local players = game.Players:GetPlayers()
local SearchString = "Simply"
local CompletedString = ""

for i,v in pairs(players) do
    if string.match(v.Name,SearchString) ~= nil then
        CompletedString = v.Name
        break
    end
end

This works because as according to the wiki: string.match will return the string that was found or nil.

So if we check the name against our search criteria we will either get nil or a valid string. If we get a valid string we know we can set the completed string to v.Name

The drawback of this specific method being that we return on the first object found, meaning there could be another player with the same name.

Another potential drawback is that this won't necessarily search from the start of a string, and could find the pattern at any point in the string.

Ad

Answer this question