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
elseif #matches == 1 then
return matches[1]
else
end
end
#Edit
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
elseif #matches == 1 then
return matches[1]
else
end
end
1 comment
Use a loop to start with, for i,v in pairs(game.Players;GetPlayers() end) and than add string things to it, for i,plr in pairs(game.Players:GetPlayers()) do if plr.Name:lower():sub(1,PlrName:len()) == PlrName:lower() then -- FOUND PLAYER end end)