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.
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
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
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)
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
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)
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)
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
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.