I'm currently making an admin but I'm not so good with strings, I am unsure on how to make a command that goes like this \/
:kill xii
"xiiHearth has been killed."
This is what I have so far
1 | function findPlayer(name) |
2 | for _, player in pairs (game.Players:GetPlayers()) do |
3 | if player.Name:lower() = = name:lower() then |
4 | return player |
5 | end |
6 | end |
7 | end |
01 | function findPlayer(name) |
02 | local data = "" |
03 | for _, player in pairs (game.Players:GetPlayers()) do |
04 | data = "" |
05 | for i = 1 , string.len(name) do |
06 | data = data..string.sub(player.Name, i, i) |
07 | end |
08 | if string.lower(data) = = string.lower(name) then |
09 | return player |
10 | end |
11 | end |
12 | end |
I think this is what you actually want, this would return the player with the a part of it's name, it works same as you want your function to work, I am not explaining stuffs because other answers already explained them. I hope it would help, thanks for reading.
I believe you are looking for string patterns.
You will likely want to use :match()
I may not be right but I believe the code for something like this would look something like this.
1 | local playerName = player.Name:lower() |
2 |
3 | if playerName:match( "^%a+" ) = = target.Name:lower() then |
4 | print ( "target Found" ) |
5 | end |
This basically lowers the players name, the "^" anchors the match to start at the start of the string, the '%a' implies all characters that match and the "+" means one or more characters of. Then it compares to the target name. (I may be wrong just tell me if I am)
The string.sub(s, i, [j])
function returns a substring of s
, starting at position i
until position j
. If j
is omitted, the substring is from position i
to the end of s
.
For example
string.sub("Hello", 2, 4)
would yield "ell"
string.sub("Hello", 2)
would yield "ello"
01 | function findPlayer(name) |
02 | matches = { } |
03 | for _, player in pairs (game:GetService( "Players" ):GetPlayers()) do |
04 | n = player.Name:lower() |
05 | nFragment = string.sub(n, 1 , string.len(name)) |
06 | if nFragment = = name then |
07 | table.insert(matches, player) |
08 | end |
09 | end |
10 |
11 | if #matches = = 0 then |
12 | -- no matches |
13 | elseif #matches = = 1 then |
14 | return matches [ 1 ] |
15 | else |
16 | -- ambiguous |
17 | end |
18 | end |
Much better solution using match
, thanks to SinisterMemories
01 | function findPlayer(name) |
02 | matches = { } |
03 | for _, player in pairs (game:GetService( "Players" ):GetPlayers()) do |
04 | if (string.match(player.Name, ".*" .. name .. ".*" )) then |
05 | table.insert(matches, player) |
06 | end |
07 | end |
08 |
09 | if #matches = = 0 then |
10 | -- no matches |
11 | elseif #matches = = 1 then |
12 | return matches [ 1 ] |
13 | else |
14 | -- ambiguous |
15 | end |
16 | end |
You're likely wanting to fetch the first player whose username starts with the search you've input.
01 | local players = game:FindService( "Players" ) |
02 |
03 | local function findPlayers(search) |
04 | search = search:lower() |
05 | local found = { } |
06 | for _, player in pairs (players:GetPlayers()) do |
07 | if player.Name:sub( 1 , #search):lower() = = search then |
08 | found [ #found + 1 ] = player |
09 | end |
10 | end |
11 | return found |
12 | end |
13 |
14 | local players = findPlayers( "Yan" ) -- findPlayers will always return a table, empty or not |
15 | print (string.format( "Found %d player%s" , #players, #players = = 1 and "" or "s" )) -- No need to worry about string.format, you don't need to use that. |
16 |
17 | -- Loop through them |
18 | for _, player in pairs (players) do |
19 | print (player.Name) |
20 | end |
You don't need to use any other string functions.