Kicking a player regardless of capitalization

Hello,

I am making a custom admin panel, and am making a kick feature.
I’m making it so that players can just type in the short name, example:
chicken instead of ChickenCrat, however, I have an issue when it comes to the capitalization.

My script finds chickencrat, but doesn’t kick since the name has capitals and I’m not sure how to get around this.
Capture

I’m sure the solution is rather simple, I’ve looked everywhere but couldn’t find it.
To summarise, I want something that could kick people regardless of capitals,
player types chi, script gets chickencrat, ChickenCrat gets kicked even with the 2 capitals.

Thank you greatly.

2 Likes
function kickNameMatches(inputName, playerName)
    return nil ~= string.find(playerName:lower(), inputName:lower())
end

print(kickNameMatches("chicK", "chickenTest")) --true
print(kickNameMatches("chick", "chickenTest")) --true
print(kickNameMatches("chickEnTeSt", "chickenTest")) --true 
print(kickNameMatches("chickenTest1", "chickenTest")) --false
print(kickNameMatches("chiskenTest", "chickenTest")) --false

EDIT: Or in words:
Compare the lower-case version of the two strings instead of the actual strings.

EDIT: Version that only matches start of name (prevents accidentally kicking wrong player)

function kickNameMatches(inputName, playerName)
    return nil ~= string.match(playerName:lower(), "^" .. inputName:lower())
end

print(kickNameMatches("chick", "chickenTest")) --true
print(kickNameMatches("chickEnTeSt", "chickenTest")) --true 
print(kickNameMatches("enTest", "chickenTest")) --false
print(kickNameMatches("chickenTest1", "chickenTest")) --false
print(kickNameMatches("chiskenTest", "chickenTest")) --false
2 Likes

Hello,

I’ve tried your code, and it says "True isn’t a valid member of Players “Players”
Would you know a way to change the boolean to a string, which I can kick with?

Thanks,
ChickenCrat

You should provide the script and/or function you’re working with so that a relevant solution can be provided.

Try this:

the function works like this, even shortned names work:

function getUser(stringName)
	stringName = string.lower(stringName)
	for i, v in pairs(game.Players:GetPlayers()) do
		if string.sub(string.lower(v.Name),1,#stringName) == stringName then
			return v
		end
	end
	return nil
end
1 Like

Hello,
The code I am working with is below:

local kick = game.ReplicatedStorage.remoteEvents.playerKick
kick.OnServerEvent:Connect(function(plr, name)
	local shortName = name
	shortName = string.lower(shortName)

	for _,plr in pairs(game.Players:GetChildren()) do
		local fullName = string.lower(plr.Name)
		if shortName == string.sub(fullName,1,string.len(shortName)) then
			game.Players[fullName]:Kick("You have been kicked from the game by an Admin")
		else
			game.ReplicatedStorage.remoteEvents.kickClient:FireClient(plr)
		end
	end
end)

Hello,

I’ve tried your code but it returns with invalid argument #2 (string expected, got Instance).

Thanks,
ChickenCrat

local kick = game.ReplicatedStorage.remoteEvents.playerKick
kick.OnServerEvent:Connect(function(plr, name)
	local foundPlayers = {}
	local lowerSearch = string.lower(name)
	
	for _, player in pairs(game.Players:GetPlayers()) do
		local lowerName = string.lower(player.Name)
		if string.match(lowerName, "^"..lowerSearch) then
			table.insert(foundPlayers, player)
		end
	end
	
	if #foundPlayers == 1 then
		foundPlayers[1]:Kick("You have been kicked from the game by an Admin")
	else
		game.ReplicatedStorage.remoteEvents.kickClient:FireClient(plr)
	end
end)

This will also prevent ambiguous searches from kicking players (a search term which matches the names of multiple players).

1 Like

Yes, it worked!

Thank you very much.