Simple Point Script

Hello developers! :smiley:

So I am writing a points system and it works fine! However, how do I make it so that you only need to type like the first letter or first few letters of a player’s name to give them points?

Can someone provide me some feedback as to how to do this? I would appreciate it a lot! I cannot find a solution on my own.

Here is my code:

local GroupId = 10421757
local MinimumRankToUseCommand = 10

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

			if Words[1] == "!addpoint" then
				local NameOfPlayerToGivePoint = Words[2]
				local AmountOfPointsToGive = 1

				local PlayerToGivePoint = game.Players:FindFirstChild(NameOfPlayerToGivePoint)

				if PlayerToGivePoint then
					PlayerToGivePoint.leaderstats.Points.Value = PlayerToGivePoint.leaderstats.Points.Value + AmountOfPointsToGive
					
					if PlayerToGivePoint.leaderstats.Points.Value == 7 then
						PlayerToGivePoint.leaderstats.Points.Value = PlayerToGivePoint.leaderstats.Points.Value - AmountOfPointsToGive
					end
				end
			end
		end)
	end
end)

Hello, @NikoFromNikoLand! Great question!

This may help you! I’ll also attach below an example of what the code should look like. I can’t prevent you from just taking it, but I do hope you read it through and try to understand it!

Code
local GroupId = 10421757
local MinimumRank = 10

game.Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinimumRank then
		Player.Chatted:Connect(function(Message)
			local Words = string.split(Message, " ")
			
			if Words[1] == "!addpoint" then
				local PlayerToGivePoint = Words[2]:lower()
				local Amount = Words[3]
				local Recipient = nil
				
				for x,y in pairs(game.Players:GetChildren()) do
					local PlayerName = y.Name:lower()
					if PlayerName:find(PlayerToGivePoint) then
						Recipient = y
					end
				end
				
				if Recipient then
					Recipient.leaderstats.Points.Value = Recipient.leaderstats.Points.Value + Amount
				end
			end
		end)
	end
end)

Typically whenever Im doing something with autofilling names I just use :match(), the “^” beginning operator and a loop through all players
(I figured I might send this when there already is an answer to point out one potentially fatal flaw with it, that flaw being that it searches any part of the name, not just the beginning, so I will offer a potential other solution in case you would like to use this option instead)

Heres a sample code for what I mean:

function autofillPlayerName(nameInput)
    local target
    for i, v in next, game.Players:GetPlayers() do
        if v.Name:match("^"..nameInput) then --^ means match at the beginning of the string
            target = v
        end
    end
    return target
end

This is essentially the same as the above persons answer and can be implemented in the same way, its just better practice if you would replace :find( with :match(“^”,

2 Likes

Thank you for the help! I read through the script and adapted to the knowledge within the script. I understand now.:slight_smile:

This helps too! Thank you greatly.