How to provide a shortened name in commands

Hello everyone!

A month or two ago I created an add point and remove point script which is used by saying
!ap [Full Username and it would add one point on to the players leaderboard.

However, some usernames are difficult and long so I was wondering if there was a way I could make it where you only have to put a few letters of a players username and the script would use that to match it to a player? Like most admin modules do.

Here is the the addpoint and remove point script. How would I make it so I can abbreviate usernames instead of needing their full username.

local Players = game:GetService("Players")

local GroupId = 10468469 
local MinimumRankToUseCommand = 8 

Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
		Player.Chatted:Connect(function(Message)
			local Words = string.split(Message, " ")
			local Command = Words[1]:lower()

			if Command == "!ap" then
				local NameOfPlayerToGivePoint = Words[2]
			

				local PlayerToGivePoint = Players:FindFirstChild(NameOfPlayerToGivePoint)

				if PlayerToGivePoint then
					PlayerToGivePoint.leaderstats.Points.Value = PlayerToGivePoint.leaderstats.Points.Value + 1
				end
			elseif Command == "!bp" then
				local NameOfPlayerToGivePoint = Words[2]

				local PlayerToGivePoint = Players:FindFirstChild(NameOfPlayerToGivePoint)

				if PlayerToGivePoint then
					PlayerToGivePoint.leaderstats.Points.Value = PlayerToGivePoint.leaderstats.Points.Value - 1
				end
			end
		end)
	end
end)

Thanks!

1 Like
local Players = game:GetService("Players")

local function GetPlayer(text)
	for _,Player in pairs(Players:GetPlayers()) do
		if string.sub(string.lower(Player.Name),1,string.len(text)) == string.lower(text) then
			return Player
		end
	end
end

In your code you would use it like so:
local PlayerToGivePoint = GetPlayer(NameOfPlayerToGivePoint)

3 Likes