Questions about string.match()

Hello-

Im trying to make it so players that use the teleport command below dont need to type out the target player’s full username. I would think, that in this instance, string.match() would work well. However I’m not quite sure how to use this & where to put it.

Could someone help me with this?

Example-

Expected Behavior:
:to MrGamingZB

Desired Behavior:
:to mrga

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Player:GetRankInGroup(adminGroupID) >= adminGroupRank then
			if string.sub(Message, 1, 1) == ":" then
				local splitString = string.split(Message, " ")
				if splitString[1] == ":to" or splitString[1] == ":To" then
					local targetPlayer = splitString[2]
					Player.Character.HumanoidRootPart.CFrame = game.Players:FindFirstChild(targetPlayer).Character.HumanoidRootPart.CFrame
				end
			end
		end
	end)
end)

EDIT: Thank you for all the responses! I’ll be chacking these solutions in about 2 hours.

4 Likes

You don’t need string.match in this case. You could use this though:

local function getPlayer(partialName)
    local plr
    for _, player in pairs(game.Players:GetPlayers()) do
        if string.find(string.lower(player.Name), string.lower(partialName)) then plr = player end
    end
    return plr
end
3 Likes

Im not good at strings, but you should be able to do a for loop and match the string, e.g

local yourstring = "pro" -- change this to whatever string
for _, plr in pairs(game.Players:GetPlayers()) do
      if tostring(plr.Name):match(yourstring) then
            print(plr.Name)
      end
end
1 Like
game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Player:GetRankInGroup(adminGroupID) >= adminGroupRank then
			if string.sub(Message, 1, 1) == ":" then
				local splitString = string.split(Message, " ")
				if splitString[1] == ":to" or splitString[1] == ":To" then
                    local targetPlr = nil
                    for _, plr in pairs(game:GetService("Players")) do
                        if string.match(plr.Name, "^" .. splitString[2]) then
                            targetPlr = plr
                        end
                    end
                    if not targetPlr then
                        return
                    end
                    Player.Character.HumanoidRootPart.CFrame = targetPlr.Character.HumanoidRootPart.CFrame
				end
			end
		end
	end)
end)
2 Likes
local function GetPlayer(name)
	name = name:gsub('%s+', '') or name
	for i, player in pairs(game:GetService('Players'):GetPlayers()) do
		if player.Name:lower():match('^'.. name:lower()) then
			return player
		end
	end
	return nil
end

This returns player

1 Like

Fixing @CipherFunctions script now it should Work fine

local adminGroupID = 0 -- GroupID

local adminGroupRank = 255 --GroupRank

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Player:GetRankInGroup(adminGroupID) >= adminGroupRank then
			if string.sub(Message, 1, 1) == ":" then
				local splitString = string.split(Message, " ")
				if splitString[1] == ":to" or splitString[1] == ":To" then
					local targetPlr = nil
					for _, plr in pairs(game:GetService("Players"):GetChildren()) do
						if string.match(plr.Name, "^" .. splitString[2]) then
							targetPlr = plr
						end
					end
					if not targetPlr then
						return
					end
					Player.Character.HumanoidRootPart.CFrame = targetPlr.Character.HumanoidRootPart.CFrame + Vector3.new(5,10,0)
				end
			end
		end
	end)
end)
2 Likes

Here is my code. Seems the most efficient.

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Player:GetRankInGroup(adminGroupID) >= adminGroupRank then
			if string.sub(Message, 1, 1) == ":" then
				local splitString = string.split(Message, " ")
				if splitString[1] == ":to" or splitString[1] == ":To" then
					local targetPlayer = splitString[2]
					for i, player in ipairs((game.Players:GetPlayers())) do
						if string.lower(player.Name):sub(1, #targetPlayer) == string.lower(targetPlayer) then
							Player.Character.HumanoidRootPart.CFrame = game.Players:FindFirstChild(targetPlayer).Character.HumanoidRootPart.CFrame
						end
					end
				end
			end
		end
	end)
end)
1 Like

Recommend making it just a function that gets it from partial name also wdym by it’s the most efficient

1 Like

It seems to be the most efficient as it has the least amount of operations.

1 Like

Well technically in that case my reply has the “most efficient” as it has the same amount of string manipulation as yours but using string.lower or string.sub as opposed to myStr:sub is 30% faster

1 Like

Elapsed CPU time your script: 0.0023153
Elapsed CPU time my script: 0.0001977

My script is 91.46114974301386% faster :stuck_out_tongue:

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

local Admins = {}
local GroupId, GroupRank = 0, 0 --These need to be changed.

local function OnPlayerAdded(Player)
	local function OnPlayerChatted(Message)
		local Clock = os.clock() --Remove this line.
		if not Admins[Player.UserId] then return end
		local Character = Player.Character
		if not Character then return end
		local Pivot = Character:GetPivot()
		Message = string.lower(Message)
		local Name = string.match(Message, "^:to%s[%w_]+")
		if not Name then return end
		for _, _Player in ipairs(Players:GetPlayers()) do
			if _Player == Player then continue end
			local _Character = _Player.Character
			if not _Character then continue end
			if not string.find(string.lower(_Player.Name), Name) then continue end
			_Character:PivotTo(Pivot)
		end
		print(os.clock() - Clock) --Remove this line.
	end
	
	Player.Chatted:Connect(OnPlayerChatted)
	local Success, Result = pcall(function() return Player:GetRankInGroup(GroupId) end)
	if not Success then warn(Result) return end
	if Result < GroupRank then return end
	Admins[Player.UserId] = true
end

local function OnPlayerRemoving(Player)
	Admins[Player.UserId] = nil
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

Not sure how you performed your benchmark, but this script yielded the following result.
image

2 Likes

That would only fix it if the person wanted the command to be group locked, though. They may want it username locked or owner locked.

1 Like

go back and check the scripts again i did fixed it

Read what I said again, I was just saying it would only be a fix if they wanted it to be group rank bound. :sob:

1 Like