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

How to find a player without typing their full name in a textbox?

Asked by 8 years ago

What I mean:

When you type "Magic", it finds the player who starts with "Magic" and goes to "Magicboopoo".

How do I do this?

I want to complete my admin GUI with new features. And this is one I've hoped for for a while.

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

You can use string patterns to accomplish this. All you need to do is loop through all the players in the game and check to see if there is a substring (anchored to the beginning using the ^ anchor) that matches what was given. It may also be convenient to ignore case using the lower method in the string library.

Example assuming you are in the game:

local function getPlayerFromSubstring(substring)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if string.lower(player.Name):match("^"..string.lower(substring)) then
            return player
        end
    end
    return nil
end

print(getPlayerFromSubstring("Magic"))
-- Output: Magicboopoo
0
deleted comment lol User#12753 0 — 8y
Ad

Answer this question