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
function findPlayer(name) for _, player in pairs(game.Players:GetPlayers()) do if player.Name:lower() == name:lower() then return player end end end
function findPlayer(name) local data = "" for _, player in pairs(game.Players:GetPlayers()) do data = "" for i = 1, string.len(name) do data = data..string.sub(player.Name, i, i) end if string.lower(data) == string.lower(name) then return player end end 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.
local playerName = player.Name:lower() if playerName:match("^%a+") == target.Name:lower() then print("target Found") 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"
function findPlayer(name) matches = {} for _, player in pairs(game:GetService("Players"):GetPlayers()) do n = player.Name:lower() nFragment = string.sub(n, 1, string.len(name)) if nFragment == name then table.insert(matches, player) end end if #matches == 0 then -- no matches elseif #matches == 1 then return matches[1] else -- ambiguous end end
Much better solution using match
, thanks to SinisterMemories
function findPlayer(name) matches = {} for _, player in pairs(game:GetService("Players"):GetPlayers()) do if (string.match(player.Name, ".*" .. name .. ".*")) then table.insert(matches, player) end end if #matches == 0 then -- no matches elseif #matches == 1 then return matches[1] else -- ambiguous end end
You're likely wanting to fetch the first player whose username starts with the search you've input.
local players = game:FindService("Players") local function findPlayers(search) search = search:lower() local found = {} for _, player in pairs(players:GetPlayers()) do if player.Name:sub(1, #search):lower() == search then found[#found + 1] = player end end return found end local players = findPlayers("Yan") -- findPlayers will always return a table, empty or not 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. -- Loop through them for _, player in pairs(players) do print(player.Name) end
You don't need to use any other string functions.