Banning players if user contains something from table

Does anybody know if theres a way to make a table, and if someone joins it bans them if something in the table is in their user?

For example:

local banned = {
    "sus",
    "amongus"
}

game.Players.PlayerAdded:Connect(function(plr)
    if plr.Name:match(table.find(banned,plr.Name)) then
        plr:Kick()
    end
end)

This script obviously doesn’t work btw, just something like this is what i have in mind. Thanks!

Do a for pairs loop through the table and see if

player.Name:lower():find(value)

Also I’m on mobile so that’s why I can’t give more depth.

Example:
for k,v in pairs(table) do
end

1 Like

If you want to ban anyone that has any of the words in the table inside their username, then this should work: (basically what @Jumpathy wrote, but I’m on PC)

local banned = {
	"sus",
	"amongus"
} -- Make sure everything is in lowercase

game.Players.PlayerAdded:Connect(function(plr)
	for i, v in pairs(banned) do
		if plr.Name:lower():match(v) then
			plr:Kick()
		end
	end
end)
1 Like

Wait. For i, v pairs or for k, v pairs?

Doesn’t matter it could even be

for _,v in pairs
for _,v in ipairs

Or

for var1,var2 in pairs

Stuff like that I just like using k,v or _,v

1 Like

The first variable is the index and the second is the actual value, that’s why we usually use “i” and “v”, but you can use a, b or k, v or even _, v which just means you don’t want to use the index

2 Likes

Thanks! I’m really bad at explaining things lol

1 Like

I have made a simple system that detects and finds for the words in the Players’ DisplayName. I have made it for DisplayName only to reduce some false positives. For example, Let’s say we have the following words/strings banned:

"sus", "mad"

And then a Player with the Name of “SuspiciousMadGuy” joins the game. If we were checking their Name and not DisplayName, They would instantly get kicked. That would be very bad, Since they would need to spend Robux only to change their name to be able to Play the game.

This is why i have made it to check the Player’s DisplayName. As Players can change their DisplayNames for free and there’s a Cooldown for doing so. Here’s the code that i used for it:

-- Variables
local Players = game:GetService("Players")
local BannedStrings = {
	"sus", "amongus", "sussy",
	"word", "anotherword", "morewords"
	-- You can put more words here
	-- But make sure they're lowercased.
}

-- Main
Players.PlayerAdded:Connect(function(Player: Player)
	-- You can either use the Player's name
	-- Or their DisplayName, I recommend
	-- Using DisplayName as it would be
	-- Kinda unfair someone needing to spend
	-- Robux to change their name only to be
	-- Able to play a Game.
	local PlayerName = string.lower(tostring(Player.DisplayName))
	local HasBannedString = false
	local BannedString = nil

	for WordIndex, WordString in pairs(BannedStrings) do
		if WordIndex and WordString then
			if string.match(PlayerName, WordString) then
				-- The player has the String in their
				-- Name/DisplayName, We can stop the loop now
				-- Or you can proceed to add the Words.
				-- The player may have several Banned Strings
				-- In their name, So we want to let them know
				-- All the words that have been found.
				HasBannedString = true

				if BannedString == nil then
					-- There's no previous BannedStrings
					-- Only chaning it to WordString.
					BannedString = WordString
				else
					-- There's previous BannedStrings
					-- Adding them into the list.
					BannedString = string.format("%s, %s", BannedString, WordString)
				end
			else
				-- No BannedString was found, Continue with the loop.
				continue
			end
		end
	end

	if HasBannedString and BannedString then
		-- The player has a Banned String in their name
		-- You can write a Kick message so they know why
		-- They've been kicked.
		Player:Kick("\nYou have Blacklisted word(s) in your Display Name.\nPlease, Remove to be able to Play.\nWord(s): "..BannedString)
	end
end)
1 Like