Okay so, im working on a adminsystem called SkyOS. In order to ban, teleport or kick someone i need to get them a instance and i have done that ba using a “findplayer” function. Now to my question: it works perfectly fine, but i always have to type in the entire name of a person and would like it to be so it finds me as a instance if i just type in “;kick flau” for example. Does anybody know how to do this using my function?
The Code
local function findplayer(name)
for i, player in pairs(game.Players:GetPlayers())do
if (type(name) == "string") then
if string.match(player.Name:lower(), name:lower()) then
return player
end
else
if (player.Name == name.Name) then
return player
end
end
end
return nil
end
Very simple generic for loop to check for any player whose name matches [x] amount of letters to your string.
local Players = game:GetService("Players")
local function FindPlayer(name)
for _,player in pairs(Players:GetPlayers()) do
if string.lower(string.sub(player.Name,1,#name)) == string.lower(name) then
return player
end
end
end
Alternatively if you want every player whose name’s first [x] amount of letters matches the sub string you’ve supplied, you can cache the player in a table and return the table after the for loop runs a full cycle.
local Players = game:GetService("Players")
local function FindPlayer(name)
local playersFound = {}
for _,player in pairs(Players:GetPlayers()) do
if string.lower(string.sub(player.Name,1,#name)) == string.lower(name) then
table.insert(playersFound,1,player)
end
end
return playersFound
end
local players = game:GetService("Players")
local function findplayer(name)
-- convert name to lower case and remove spaces and other
-- extraneous characters not allowed in usernames
name = string.gsub(string.lower(name), "[^a-z0-9_]", "")
for _, player in next, players:GetPlayers() do
local playername = string.lower(player.name)
if string.sub(playername, 0, #name) == name then
return player
end
end
end
That’s because it’s expecting a string (Player name), not a Player Instance. You’d have to modify as you did in your original script to accept a Player Instance.
local function findplayer(name)
for i, player in pairs(game.Players:GetPlayers())do
if (type(name) == "string") then
if string.match(player.Name:sub(1,#name):lower(), name:lower()) and #name <= #player.Name then
return player
end
else
if (player.Name == name.Name) then
return player
end
end
end
return nil
end