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

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
mNo edit summary
>NXTBoy
Seriously, use str:lower() instead of string.lower(str)
 
(31 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{CatUp|FAQ}}
{{CatUp|FAQ}}


Banning people from your [[place]] can be achieved by putting in a special command script. It can be edited much like a [[How do I make VIP doors?|VIP door]] script.
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]].


__TOC__


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


1. Open your place in [[Roblox Studio]].
1. Open your place in [[Roblox Studio]].


2. Click on [[Toolbox|Insert]] to open the toolbox.
2. Click on '''Insert''', then '''Object...'''
 
::[[Image:Insert Object.png]]
3. In the toolbox, click on '''Free Models'''.


4. Search for '''"Speak to Ban Player Script"''' (with quotes) and click on the LAST object that appears.
3. From the window that pops up, find '''Script''', then click OK.


4. Find the script you just inserted in the [[Edit#The_Explorer_and_Properties_Bar|Explorer panel]], and double-click it to open the script editor.


==Now what?==
==Now what?==


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.  
Now the contents of the script will be constructed.


In the Explorer panel, find '''BanScript''' and double-click on it. A scripting window will pop up. You can add your name to the ''speakers'' list, allowing you to use the command. You can also add your friend's name, if you want.
===Variables===


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


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.
{{lua|1=
local speakers = {"Person1", "Person2", "Person3"}
local banned  = {"Username1", "Username2", "Username3"}
}}


For example: if Builderman and Builderdude are in-game, "ban builderm" is enough. If Builderman and Telamon are the only people in the game, you can ban both by typing "ban b t"
===Functions===


Ambiguous bans are ignored. Example: Builderman and Builderdude are in-game. "ban bu" is ambiguous.
There are a few {{type|function|functions}} you'll have to add to your [[script]] for it to work properly.


====checkSpeakers====


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


Adding a player's name to the ''speakers'' list will allow them to use the command.
{{lua|1=
local function checkSpeakers(name)
for _, speaker in pairs(speakers) do
if name:lower() == speaker:lower() then return true end
end
return false
end
}}


Adding a player's name to the ''banned'' list will not allow them to enter the place.
====banPlayer====


Saying "ban telamon" will ban Telamon from the place.
This {{type|function}} is used to ban a [[player]] from your [[place]].


Saying "ban telamon builderman" will ban Telamon and Builderman from the place.
{{lua|1=
local function banPlayer(banner, victim)
if victim ~= banner then
victim:Destroy()
banned[victim.Name] = victim.Name
end
end
}}


If Telamon and Builderman were the only ones in the place, saying "ban t b" would ban them.
====matchPlayer====


If Telamon and Teladude were in the place, saying "ban tela" would not ban either of them.
This {{type|function}} is used to match a [[Player|player's]] name when the command is used.


==the final script==
{{lua|1=
this is the final script that you should have. there are some other ways but this is the way that i make them:
local function matchPlayer(str)
<pre>
local result = nil
speakers = {"?????"} -- Those who can say the command and in case there's already a name, delete that name and put there your own name
local players = game.Players:GetPlayers()
banned = {"?????","?????","?????"} -- Those who are banned
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


function check(name)
result = player
print(name)
end
        if #speakers == 0 then print("No speakers") return false end
end
        for i = 1,#speakers do
return result
                if (string.upper(name) == string.upper(speakers[i])) then return true end
        end
        return false
end
end
}}
====onChatted====
This {{type|function}} is used when a [[player]] in the {{`|speakers}} list speaks. It checks if they're trying to ban someone.


function DisplayMessage(text, Wait, parent)
{{lua|1=
local message = Instance.new("Message")
local function onChatted(msg, speaker)
message.Text = text
local source = speaker.Name:lower()
message.Name = "Message"
msg = msg:lower()
message.Parent = parent
if msg:find("ban") == 1 then -- msg starts with "ban"
wait(Wait)
-- words and numbers
if message ~= nil then
for word in msg:gmatch("%w+") do
message.Parent = nil
local p = matchPlayer(word)
if p then
banPlayer(speaker, p)
end
end
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.


function ban(name, source)
{{lua|1=
local player = game.Players:FindFirstChild(name)
local function onPlayerEntered(newPlayer)
if player ~= nil then
-- remove banned player if they try to come back in
player.Parent = nil
for _, v in pairs(banned) do
table.insert(banned,player.Name)
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
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)
}}


function BreakDown(msg)
== Altogether ==
local sep = ", "
local str = sep..msg:sub(6)
fields = {str:match((str:gsub(sep.."[^"..sep.."]*", sep.."([^"..sep.."]*)")))}


Here is the entire script put together.
{{lua|1=
local speakers = {"Person1", "Person2", "Person3"}
local banned = {"Username1", "Username2", "Username3"}


return fields
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
end


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


local norm = msg
local function matchPlayer(str)
local msg = string.lower(msg)
local result = nil
 
local players = game.Players:GetPlayers()
if not (check(speaker.Name)) then return end
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


if string.sub(msg,1,5) == "/ban " then
local function onChatted(msg, recipient, speaker)
local t = BreakDown(norm)
local source = string.lower(speaker.Name)
if msg == "/ban " then DisplayMessage("Please specify player(s)",2,speaker) return end
msg = string.lower(msg)
if #t > 0 then
if (string.find(msg, "ban") == 1) then --- msg starts with "ban"
for i = 1,#t do
-- words and numbers
print("\""..t[i].."\"")
for word in msg:gmatch("%w+") do
local player = game.Players:FindFirstChild(t[i])
local p = matchPlayer(word)
if player ~= nil then
if (p ~= nil) then
ban(player.Name, speaker)
banPlayer(speaker, p)
end
end
end
end
end
Line 109: Line 176:
end
end


function onPlayerEntered(newPlayer)
local function onPlayerEntered(newPlayer)
print("entered")
-- remove banned player if they try to come back in
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
for i,v in pairs(banned) do
for i=1, #banned do
if (v:lower() == newPlayer.Name:lower()) then
if banned[i]:lower() == newPlayer.Name:lower() then
newPlayer:Destroy()
newPlayer.Parent = nil
end
end
end
if checkSpeakers(newPlayer.Name) then
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
end
end
end
end


game.Players.ChildAdded:connect(onPlayerEntered)
game.Players.PlayerAdded:connect(onPlayerEntered)
                                                                                                            </pre>
}}
==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 [[#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==


*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 ==
* [[How_To:_Admin_Commands|How To: Admin Commands]]
* [[Chatted (Event)]]
* [[PlayerAdded (Event)]]


[[Categories: 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