How do I ban people from my place?: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Anaminus
>NXTBoy
Seriously, use str:lower() instead of string.lower(str)
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{CatUp|FAQ}}
{{CatUp|FAQ}}
__TOC__


==Introduction==
There are two ways to be able to remove annoying people from your [[Client-Side_Scripting_and_Server-Side_Scripting|servers]]. In the Insert box under Game Tools there is a Vote Kick Script from [[User:Telamon|Telamon]] which works quite well. Banning people from your [[place]] can be achieved by creating a special kind of [[How_To:_Admin_Commands|command]] script. It can be edited much like a [[How do I make VIP doors?|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 [[Toolbox#Free Models|Free Models]].


Banning people from your [[place]] can be achieved by creating a special [[Commands|command]] script. It can be edited much like a [[How do I make VIP doors?|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 [[Toolbox#Free Models|Free Models]].
__TOC__


==Steps==
==Steps==
Line 25: Line 24:
===Variables===
===Variables===


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


<pre>
{{lua|1=
speakers = {"Builderman", "Telamon", "Anaminus"}
local speakers = {"Person1", "Person2", "Person3"}
banned = {"EvilPerson", "BadUsername", "MrEnemy"}
local banned   = {"Username1", "Username2", "Username3"}
</pre>
}}


===Functions===
===Functions===


There are a few functions you'll have to add to your script for it to work properly.
There are a few {{type|function|functions}} you'll have to add to your [[script]] for it to work properly.


====checkSpeakers====
====checkSpeakers====


This function is used to check if a player who has spoken is on the ''speakers'' list.
This {{type|function}} is used to check if a [[player]] who has spoken is on the {{`|speakers}} list.


<pre>
{{lua|1=
function checkSpeakers(name)
local function checkSpeakers(name)
for i,v in pairs(speakers) do
for _, speaker in pairs(speakers) do
                if (string.upper(name) == string.upper(v)) then return true end
if name:lower() == speaker:lower() then return true end
        end
end
        return false
return false
end
end
</pre>
}}


====banPlayer====
====banPlayer====


This function is used to ban a player from your place.
This {{type|function}} is used to ban a [[player]] from your [[place]].


<pre>
{{lua|1=
function banPlayer(banner, victim)
local function banPlayer(banner, victim)
if (victim ~= banner) then
if victim ~= banner then
victim:Remove()
victim:Destroy()
banned[victim.Name] = victim.Name
banned[victim.Name] = victim.Name
end
end
end
end
</pre>
}}


====matchPlayer====
====matchPlayer====


This function is used to match a players name when the command is used.
This {{type|function}} is used to match a [[Player|player's]] name when the command is used.
 
{{lua|1=
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 {{type|function}} is used when a [[player]] in the {{`|speakers}} list speaks. It checks if they're trying to ban someone.
 
{{lua|1=
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 {{type|function}} is used when a [[player]] enters the [[Client-Side_Scripting_and_Server-Side_Scripting|server]]. It enables players on the {{`|speakers}} list to use the command, and also removes players if they're on the {{`|banned}} list.
 
{{lua|1=
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|onPlayerEntered()]] {{type|function}} when a [[player]] enters.
 
{{lua|1=
game.Players.PlayerAdded:connect(onPlayerEntered)
}}
 
== Altogether ==
 
Here is the entire script put together.
{{lua|1=
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


<pre>
local function banPlayer(banner, victim)
function matchPlayer(str)
if (victim ~= banner) then
victim:Destroy()
banned[victim.Name] = victim.Name
end
end
 
local function matchPlayer(str)
local result = nil
local result = nil
local players = game.Players:GetPlayers()
local players = game.Players:GetPlayers()
for i,v in pairs(game.Players:GetPlayers()) do
for i,v in pairs(game.Players:GetPlayers()) do
if (string.find(string.lower(v.Name), str) == 1) then
if (string.find(string.lower(v.Name), str) == 1) then
if (result ~= nil) then return nil end
if (result ~= nil) then return nil end
result = v
result = v
end
end
Line 78: Line 161:
return result
return result
end
end
</pre>
====onChatted====
This function is used when a player in the ''speakers'' list speaks. It checks if they're trying to ban someone.


<pre>
local function onChatted(msg, recipient, speaker)
function onChatted(msg, recipient, speaker)
local source = string.lower(speaker.Name)
local source = string.lower(speaker.Name)
msg = string.lower(msg)
msg = string.lower(msg)
Line 98: Line 175:
end
end
end
end
</pre>
====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.
<pre>
function onPlayerEntered(newPlayer)


local function onPlayerEntered(newPlayer)
-- remove banned player if they try to come back in
-- remove banned player if they try to come back in
for i,v in pairs(banned) do
for i,v in pairs(banned) do
if (v:lower() == newPlayer.Name:lower()) then
if (v:lower() == newPlayer.Name:lower()) then
newPlayer:Remove()
newPlayer:Destroy()
end
end
end
end
Line 117: Line 187:
end
end
end
end
</pre>
===Connections===


This is the last part of the script needed to make it work. It simply activates the [[{{FULLPAGENAME}}#onPlayerEntered|onPlayerEntered]] function when a player enters.
<pre>
game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerAdded:connect(onPlayerEntered)
</pre>
}}
 
==How does it work?==
==How does it work?==


Now that you have your completed ban script, this is how it works.
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 "<code>ban [player name]</code>" in-game to ban an abusive player from your place. You can add your name to the [[{{FULLPAGENAME}}#Variables|''speakers'']] list, allowing you to use the command. You can also add your friend's name, if you want. You can add a person's name to the [[{{FULLPAGENAME}}#Variables|''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, "<code>ban builderm</code>" is enough. If Builderman and Telamon are the only people in the game, you can ban both by typing "<code>ban b t</code>". Ambiguous bans are ignored. Example: Builderman and Builderdude are in-game. "<code>ban bu</code>" is ambiguous, because you could be referring to either one.
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 [[#Variables|{{`|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 [[#Variables|{{`|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==
==Recap==


*Adding a player's name to the ''speakers'' list will allow them to use the command.
*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.
*Adding a player's name to the {{`|banned}} list will not allow them to enter the place.
*Saying "<code>ban telamon</code>" will ban Telamon from the place.
*Saying {{`|"ban telamon"}} will ban Telamon from the [[place]].
*Saying "<code>ban telamon builderman</code>" will ban Telamon and Builderman 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 "<code>ban t b</code>" would ban them.
*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 "<code>ban tela</code>" would not ban either of them.
*If Telamon and Teladude were in the place, saying {{`|"ban tela"}} would not ban either of them.
 
== See Also ==
* [[How_To:_Admin_Commands|How To: Admin Commands]]
* [[Chatted (Event)]]
* [[PlayerAdded (Event)]]


[[Category: Scripting Tutorials]]
[[Category: Scripting Tutorials]]

Latest revision as of 07:07, 16 April 2012

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