How do I ban people from my place?

There are two ways to be able to remove annoying people from your servers. In the Insert box under Game Tools there is a Vote Kick Script from Shedletsky which works quite well. Banning people from your place can be achieved by creating a special kind of command script. It can be edited much like a VIP door script. Instead of simply giving you a script to edit, this article will take you through steps on how this script works, and how to make it yourself. If you want a ban script right away, search for one in Free Models.

Contents


[edit] Steps

These steps will show you how to insert a new script into your place.

  1. Open your place in Roblox Studio.
  2. Click on Insert, then Object...
  3. From the window that pops up, find Script, then click OK.
  4. Find the script you just inserted in the Explorer panel, and double-click it to open the script editor.

Insert Object.png

[edit] Now what?

Now the contents of the script will be constructed.

[edit] Variables

There are two variables this script uses. They will both be tables, which will include names of users. The first table is for the names of users who can use the command. The second table is for users who will never be able to enter your place. Here's what it might look like:

local speakers = {"Person1", "Person2", "Person3"}
local banned   = {"Username1", "Username2", "Username3"}

[edit] Functions

There are a few functions you'll have to add to your script for it to work properly.

[edit] checkSpeakers

This function is used to check if a player who has spoken is on the speakers list.

local function checkSpeakers(name)
	for _, speaker in pairs(speakers) do
		if name:lower() == speaker:lower() then return true end
	end
	return false
end

[edit] banPlayer

This function is used to ban a player from your place.

local function banPlayer(banner, victim)
	if victim ~= banner then
		victim:Destroy()
		banned[victim.Name] = victim.Name
	end
end

[edit] matchPlayer

This function is used to match a player's name when the command is used.

local function matchPlayer(str)
	local result = nil
	local players = game.Players:GetPlayers()
	for _, player in pairs(players) do
		if player.Name:lower():find(str) == 1 then
			--Abort if two players match the string
			if result then return end
 
			result = player
		end
	end
	return result
end

[edit] onChatted

This function is used when a player in the speakers list speaks. It checks if they're trying to ban someone.

local function onChatted(msg, speaker)
	local source = speaker.Name:lower()
        -- split command into the first word and the remaining words
	local cmd, args = msg:match("(%w+) (.+)")
 
	if cmd == "ban" then
		-- words and numbers
		for word in args:gmatch("%w+") do 
			local p = matchPlayer(word)
			if p then
				banPlayer(speaker, p)
			end
		end
	end
end

[edit] onPlayerEntered

This function is used when a player enters the server. It enables players on the speakers list to use the command, and also removes players if they're on the banned list.

local function onPlayerEntered(newPlayer)
	-- remove banned player if they try to come back in
	for _, v in pairs(banned) do
		if v:lower() == newPlayer.Name:lower() then
			newPlayer:Destroy()
		end
	end
	if checkSpeakers(newPlayer.Name) then
		newPlayer.Chatted:connect(function(msg)
			onChatted(msg, newPlayer)
		end) 
	end
end

[edit] Connections

This is the last part of the script needed to make it work. It simply activates the onPlayerEntered() function when a player enters.

game.Players.PlayerAdded:connect(onPlayerEntered)

[edit] Altogether

Here is the entire script put together.

local speakers =	{"Person1", "Person2", "Person3"}
local banned =		{"Username1", "Username2", "Username3"}
 
local function checkSpeakers(name)
	for _, speaker in pairs(speakers) do
		if name:lower() == speaker:lower() then return true end
	end
	return false
end
 
local function banPlayer(banner, victim)
	if victim ~= banner then
		victim:Destroy()
		banned[victim.Name] = victim.Name
	end
end
 
local function matchPlayer(str)
	local result = nil
	local players = game.Players:GetPlayers()
	for _, player in pairs(players) do
		if player.Name:lower():find(str) == 1 then
			--Abort if two players match the string
			if result then return end
 
			result = player
		end
	end
	return result
end
 
local function onChatted(msg, speaker)
	-- split command into the first word and the remaining words
	local cmd, args = msg:lower():match("(%w+) (.+)")
 
	if cmd == "ban" then
		-- words and numbers
		for word in args:gmatch("%w+") do 
			local p = matchPlayer(word)
			if p then
				banPlayer(speaker, p)
			end
		end
	end
end
 
local function onPlayerEntered(newPlayer)
	-- remove banned player if they try to come back in
	for _, v in pairs(banned) do
		if v:lower() == newPlayer.Name:lower() then
			newPlayer:Destroy()
		end
	end
	if checkSpeakers(newPlayer.Name) then
		newPlayer.Chatted:connect(function(msg, recipient)
			onChatted(msg, newPlayer)
		end) 
	end
end
 
game.Players.PlayerAdded:connect(onPlayerEntered)

[edit] How does it work?

Now that you have your completed ban script. This is how it works:

This script adds the "ban" command to your game. Certain players can type "ban [player name]" in-game to ban an abusive player from your place. You can add your name to the speakers list, allowing you to use the command. You can also add your friends' names if you want. You can add a person's name to the banned list if you don't want them to enter your place. You will not have to use the ban command on them since they're already banned. When banning someone, you don't need to type the player's full name, just enough letters to be sure of who you want to ban. For example: if Builderman and Builderdude are in-game, "ban builderm" to ban Buildernman is enough. If Builderman and Shedletsky are the only people in the game, you can ban both by typing "ban b s". Ambiguous bans are ignored. Example: Builderman and Builderdude are in-game. "ban bu" is ambiguous because you could be referring to either one.

[edit] Recap

  • Adding a player's name to the speakers list will allow them to use the command.
  • Adding a player's name to the banned list will not allow them to enter the place.
  • Saying "ban Shedletsky" will ban Shedletsky from the place.
  • Saying "ban Shedletsky builderman" will ban Shedletsky and Builderman from the place.
  • If Shedletsky and Builderman were the only ones in the place, saying "ban s b" ban them both.
  • If Telamon and Teladude were in the place, saying "ban tela" would not ban either of them.

[edit] See Also