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

How to filter through players with part of their name?

Asked by 5 years ago

So, I'm working on my admin commands and I'm wondering how I would go about searching through the players if I have just part of their name in a string. For example, if the string was "ender" and the player "Endergenius100" was in the game, then I would run the command on that player. So far, I have tried string.match("ender","^+"..v.Name), but that didn't work. I'm very new to string manipulation, and help would be appreciated. Thanks!

0
i got the same problem Gameplayer365247v2 1055 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

You could accomplish this by matching the the letters in this case "ender" with all the players in the player list.

For example if you wanted to have an admin command to kill someone you'd do ":kill ender"

01--// Looping through all the players in the game
02for _, Player in pairs(Players:GetChildren()) do
03    --// Making the players name lowercase
04    local makeLower = string.lower(Player.Name)
05 
06    --// Use :find() to find any player name that matches whatever you typed
07    --// INPUT is whatever you used to break up the string message to get
08    --// the players name
09    if makeLower:find(INPUT) then
10        --// Just as an example you can get their character ect.
11        local Char = Player.Character
12        local Hum = Char.Humanoid
13 
14        Hum.Health = 0
15    end
16end

So this would kill every player that matches so if you did ":kill s" it would kill every player who has a name starting with the letter s.

In some cases this is nice but if you don't want that to happen you could:

01for _, Player in pairs(Players:GetChildren()) do
02    local makeLower = string.lower(Player.Name)
03    if makeLower:find(INPUT) then
04        local Char = Player.Character
05        local Hum = Char.Humanoid
06        Hum.Health = 0
07        --// Add a break here to stop the code from looking for more matches
08        break
09    end
10end
0
Thanks so much! I had no idea there was a ":find" function. Endergenius100 12 — 5y
0
No problem, glad it helped. SimpleFlame 255 — 5y
Ad

Answer this question