Have you ever tried creating an admin script, but always wondered how developers make it so you can abbreviate the spelling of a players name? For example, how they can do “:kick Rob” instead of “:kick Robert2311”. In this tutorial, I’ll be giving you the basic code on how to create a function that does just this, going through the steps on each line.
The function will look somewhat like this:
GetPlayerFromAbbreviation("pro")
--> programher (Instance)
Here’s the code for the function:
function GetPlayerFromAbbreviation(abbreviation)
for i,v in ipairs(game:GetService("Players"):GetPlayers()) do -- looping through players
if v.Name:lower():sub(1,#abbreviation) == abbreviation:lower() then -- checking if the lowercase version of the players name (subbed) matches the lowercase version of the abbreviation
return v -- if it does, return the players instance
else
if #game:GetService("Players"):GetPlayers() == i then -- checking if the index matches the player count (AKA if there are no more players to loop through)
return false -- returns false if no players matching the abbreviation was found
else
continue -- continues the loop if there are still players to loop through
end
end
end
end
This is a basic example of how you can use this in an admin script:
local admins = {"programher"}
game:GetService("Players").PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
local args = message:split(" ")
if args[1]:lower() == ":kick" and admins[player.Name] then
if args[2] then
if GetPlayerFromAbbreviation(args[2]) ~= false then -- checking for the player
local target = GetPlayerFromAbbreviation(args[2]) -- player exists, set the target value to the function (with args[2] as the parameter)
target:Kick("You were kicked by an administrator.")
end
end
end
end)
end)
Remember, GetPlayerFromAbbreviation returns the players instance. Hope this helps!