How do I ban people from my place?

From Legacy Roblox Wiki
Jump to navigationJump to search

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

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.

Now what?

Now the contents of the script will be constructed.

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"}

Functions

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

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

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

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

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()
	msg = msg:lower()
	if msg:find("ban") == 1 then -- msg starts with "ban"
		-- words and numbers
		for word in msg:gmatch("%w+") do 
			local p = matchPlayer(word)
			if p then
				banPlayer(speaker, p)
			end
		end
	end
end

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, recipient)
			onChatted(msg, newPlayer)
		end) 
	end
end

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)

Altogether

Here is the entire script put together.

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

local function checkSpeakers(name)
	for i,v in pairs(speakers) do
		if (string.upper(name) == string.upper(v)) 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 i,v in pairs(game.Players:GetPlayers()) do
		if (string.find(string.lower(v.Name), str) == 1) then
		if (result ~= nil) then return nil end
			result = v
		end
	end
	return result
end

local function onChatted(msg, recipient, speaker)
	local source = string.lower(speaker.Name)
	msg = string.lower(msg)
	if (string.find(msg, "ban") == 1) then --- msg starts with "ban"
		-- words and numbers
		for word in msg:gmatch("%w+") do 
			local p = matchPlayer(word)
			if (p ~= nil) then
				banPlayer(speaker, p)
			end
		end
	end
end

local function onPlayerEntered(newPlayer)
	-- remove banned player if they try to come back in
	for i,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, recipient, newPlayer) end) 
	end
end

game.Players.PlayerAdded:connect(onPlayerEntered)

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 Telamon are the only people in the game, you can ban both by typing "ban b t". Ambiguous bans are ignored. Example: Builderman and Builderdude are in-game. "ban bu" is ambiguous because you could be referring to either one.

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 telamon" will ban Telamon from the place.
  • Saying "ban telamon builderman" will ban Telamon and Builderman from the place.
  • If Telamon and Builderman were the only ones in the place, saying "ban t b" ban them both.
  • If Telamon and Teladude were in the place, saying "ban tela" would not ban either of them.

See Also