How do I get a certain player from only a few characters?

Okay so, I have a script that will kick a player if an admin provides their correct name. How would I make it so that let’s say the player’s name is “EpicGamerMan2929”, if an admin submits “ep” / “epic” / “Epic” etc… it will find the player that starts with that no matter if it’s uppercase or lowercase? This is what I have so far:

local event = game.ReplicatedStorage.admin
event.OnServerEvent:Connect(function(player,target,action)
	if player:FindFirstChild("Admin") then
		if action == "kick" then
			if game.Players:FindFirstChild(target) then
				local plr = game.Players:FindFirstChild(target)
				plr:kick("Kicked by an admin")
			end
		end
	end
end)

Thanks

6 Likes

You can find players by doing something similar to this:

local event = game.ReplicatedStorage.admin
event.OnServerEvent:Connect(function(player,target,action)
   if player:FindFirstChild("Admin") then
   	if action == "kick" then
   			local plr
             for i,v in pairs(game.Players:GetPlayers()) do 
                 if v.Name:lower():sub(1,#target) == target:lower() then
                     plr = v
   			         plr:kick("Kicked by an admin")
                 end
             end
   		end
   	end
   end
end)

Note, it will only get the last player if there are two players with similar names.

17 Likes

Ah I see, makes sense, thanks a bunch!

Let me know if you have any problems, I apologize as there might be formatting issues as I wrote that on mobile.

This line should be removed as it prevents the if statement’s body from being ran if a player cannot be found with the same name as target (making the rest of the code redundant).

This isn’t true; each player a matching name (assuming you fix the previous error) will be kicked with your current code. For only one player to be kicked, you’d need to break once you kick a player or move the kick to after the for statement.

Fixed code
local event = game:GetService("ReplicatedStorage").admin
event.OnServerEvent:Connect(function(player, target, action)
	if player:FindFirstChild("Admin") ~= nil then
		if action == "kick" then
			target = target:lower()
			for _, player in ipairs(game:GetService("Players"):GetPlayers()) do 
				if player.Name:lower():sub(1, #target) == target then
					player:Kick("Kicked by an admin")
					break
				end
			end
		end
	end
end)
7 Likes

You should try creating a function to return the player by using shortcut name

e.g.

function getPlayer(shortcut)
  local player = nil

  local g = game.Players:GetPlayers()
  for i = 1, #g do
    if string.lower(string.sub(g[i].Name, 1, string.len(shortcut))) == string.lower(shortcut) then
      player = g[i]
      break
    end
  end

  return player

end

So, to retrieve the player, you just call the function

local plr = getPlayer(“epi”)
print(plr.Name) -- prints - EpicGamerMan2929

However, if there are 2 people that have ‘epi’ at the beginning of their username, it will retrieve the first person that goes in the loop.

so your code

if action == "kick" then
	target = getPlayer(target)
	if target ~= nil then
		target:Kick("Kicked by an admin")
	end
end
7 Likes

That would make everything more clean, especially when used in a module script, thanks!

What happens if you want to kick several players with the same starting name? Or if you kick the wrong person?

1 Like

This line was not meant to be in my code, I meant to delete it as it was in OP’s code, probably missed it as I am on mobile, good catch though.

1 Like