User:JulienDethurens/Draft: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
>JulienDethurens
Blanked the page
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Introduction==
Restricted doors, also commonly called <abbr title="Very Important Person">VIP doors</abbr>, are used to restrict access to a certain area to only certain users. There are many restrictions that one could want to apply to a restricted door, and this article can not cover them all. It will however cover the most common restrictions:
*Being a certain player.
*Owning a certain asset (usually a t-shirt, but can also be a badge, a shirt, or even a hat).
*Being in a specific group.
*Being the creator of the place.
*Being a friend or best friends of a certain player or of the creator.
However, you should always think before making a restricted door that restricts access to a certain area. Ask yourself these questions:
*Is it really necessary?
*Wouldn't it be better to make a GUI or a command that teleports there?
*Isn't there a nicer way to do it?
The problem is that restricted doors are often overused. Sometimes, they are used to protect a room that contains certain items. Yet, these items could be given automatically to the players, instead of them having to enter the room every time they die. Try to avoid using a restricted door if you can do it in a cleaner way.
==Building the Door==
[[File:Door.png|thumb|An example of a door.]]
Building the door should not be a problem, but I am still going to describe it. First, you will need to create the door. Usually, you will want it to be of the size of a character, so players can get through. You will probably want the door to be anchored too, unless you expect it to move whenever a player touches it...


The size of a player's character is 4 x 5 x 1, so you will probably want your door to have that size. However, you can't get exactly that size with the Brick [[FormFactor (Enum)|FormFactor]]. Therefore, you will probably want to change the FormFactor to Symmetric, so you can get the exact size. Another solution would be to simply make the door slightly bigger than the exact size of a character. Usually, you will also want to build a room around the door, but that is out of the scope of this tutorial.
==Scripting the Door==
===Structure===
First, we will need to create a script in the door. Go in the "Insert" menu and choose "Object...". Then, use the dialog that appears to insert a script in the door. Once you have a script in your door, open it to edit it in the script editor.
Because the door will be manipulated a lot in the script, it would be a good idea to define a variable that contains a reference to it. I'll call that variable <var>door</var>, but you can call it whatever you want.
<code lua>
local door = script.Parent
</code>
You will probably want to be able to open and close the door. Let's make a function to open the door and another to close it.
There are many ways to open a door, but, for this tutorial, we will choose one that makes the door progressively disappear.
We will use a loop to make the door fade, and we will use its [[Transparency (Property)|Transparency]] property to change its opacity. Then, finally, we will change the door's [[CanCollide (Property)|CanCollide]] property to allow players to walk through it.
In case you weren't sure how to write the code, here is an example that works. However, you are encouraged to write your own code, as that is how you will learn.
<code lua>
local door = script.Parent
function open()
-- This function will open the door.
door.CanCollide = false -- Make players able to walk through the door.
for transparency = 0, 1, .1 do
door.Transparency = transparency
wait(.1)
end
end
</code>
If there is an open function, there is of course also a close function. That close function does exactly the opposite of what the open function does, so you should be able to write the code yourself. If not, here is the code as I wrote it, but remember you are encouraged to write it yourself.
<code lua>
local door = script.Parent
function open()
-- This function will open the door.
door.CanCollide = false -- Make players able to walk through the door.
for transparency = 0, 1, .1 do
door.Transparency = transparency
wait(.1)
end
end
function close()
-- This function will close the door.
for transparency = 1, 0, -.1 do
door.Transparency = transparency
wait(.1)
end
door.CanCollide = true -- Make players unable to walk through the door.
end
</code>
You probably want the door to open when it is touched, and, therefore, it should use the [[Touched (Event)|Touched]] event. Therefore, let's connect the Touched event to a function. Then, we will check if the part that touched the door is in a character. Here is the code that I wrote.
<code lua>
local door = script.Parent
function open()
-- This function will open the door.
door.CanCollide = false -- Make players able to walk through the door.
for transparency = 0, 1, .1 do
door.Transparency = transparency
wait(.1)
end
end
function close()
-- This function will close the door.
for transparency = 1, 0, -.1 do
door.Transparency = transparency
wait(.1)
end
door.CanCollide = true -- Make players unable to walk through the door.
end
door.Touched:connect(function(part)
if not part.Parent then return end -- We don't want the script to error just because the part has no parent.
local player = game:GetService('Players'):GetPlayerFromCharacter(part.Parent) or game:GetService('Players'):GetPlayerFromCharacter(part.Parent.Parent)
if not player then return end -- If it isn't a character that touched the door, then we ignore it.
end)
</code>
Now, what do we want to do when the door is touched? We want to check if the player corresponds to one of the restrictions. That brings me to the next section...
===Restrictions===
There are many ways to check if the player corresponds to one of the restrictions, but we will use a simple system in this case. However, you are encouraged to try other ways to do it, as that will help you to learn.
The system we will use in this example is simple. We will use a table that contains all the restrictions, under the form of boolean expressions. If any of these boolean expressions is true, then the door will open.
What we will need is a table and a loop that iterates through the table. If any of the values in the table is true, it will open the door, wait 4 seconds and then close it. You can see the code I wrote below.
<code lua>
local door = script.Parent
function open()
-- This function will open the door.
door.CanCollide = false -- Make players able to walk through the door.
for transparency = 0, 1, .1 do
door.Transparency = transparency
wait(.1)
end
end
function close()
-- This function will close the door.
for transparency = 1, 0, -.1 do
door.Transparency = transparency
wait(.1)
end
door.CanCollide = true -- Make players unable to walk through the door.
end
door.Touched:connect(function(part)
if not part.Parent then return end -- We don't want the script to error just because the part has no parent.
local player = game:GetService('Players'):GetPlayerFromCharacter(part.Parent) or game:GetService('Players'):GetPlayerFromCharacter(part.Parent.Parent)
if not player then return end -- If it isn't a character that touched the door, then we ignore it.
local restrictions = {}
for _, restriction in pairs(restrictions) do
if restriction then
open()
delay(4, close)
end
end
end)
</code>
Now, we need to put some restrictions in there. A restriction, in this case, is
Let's see some common restrictions you might want to apply.
====Being a Certain Player====
Often, you will want to authorize certain specific players to go through the door. In a such case, this is probably what you would do.
You have probably already guessed how to apply this restriction, but, if you didn't, it is simple. You just check if the player's name is equal to a certain string:
<code lua>
player.Name == "PlayerName"
</code>
====Being in a Certain Group====
Checking if a player is in a certain group is easy. You have to use the [[IsInGroup (Method)|IsInGroup]] method of the [[RBX.lua.Player (Object)|Player]] object.
Incase you don't already know what the IsInGroup method does, I'll explain:
The IsInGroup method accepts one argument, which is a group's <abbr title="identifier">id</abbr>. If the player is in the group with that <abbr title="identifier">id</abbr>, the method will return true. Otherwise, it will return false.
Therefore, here is an example of applying this restriction (note: you can change 127081 to the <abbr title="identifier">id</abbr> of any group):
<code lua>
player:IsInGroup(127081)
</code>
====Owning a Certain Asset====
You might already know that it is possible to verify if a player has a certain badge with the [[UserHasBadge (Method)|UserHasBadge]] method of the [[RBX.lua.BadgeService (Object)|BadgeService]]. But what you might not know is that this method can actually be used to know if the user is the owner of <strong>any</strong> asset, including hats, gear, badges, t-shirts, shirts, pants and even places!
Restricted doors often use t-shirts, and, in the past, the BadgeService did not exist, so scripters had to check if the player was wearing the t-shirt, which forced the users to constantly change their t-shirts, and which was annoying.
Since the release of the BadgeService, and since the discovery of the fact that it can actually be used to know if the user owns any asset, more and more users are using it for restricted doors and other kinds of scripts.
The UserHasBadge method has two arguments, <var>userId</var> and <var>badgeId</var>. The <var>userId</var> argument is the <abbr title="identifier">id</abbr> of a certain player, and the <var>badgeId</var> argument is the <abbr title="identifier">id</abbr> of a certain asset. The method will return true if the player with the specified <abbr title="identifier">id</abbr> has the asset specified by the <var>badgeId</var> argument.
Here is an example of applying this restriction:
<code lua>
game:GetService('BadgeService'):UserHasBadge(player.userId, 1032571)
</code>
'''Note: '''some older scripts that still use the method of checking if the user is wearing the t-shirt ask you to substract 1 from the <abbr title="identifier">id</abbr>. However, this method does <strong>not</strong> require you to substract one from the <abbr title="identifier">id</abbr>.
====Being the Creator of the Place====
This restriction could be achieved by simply comparing the player's name with a certain predefined name (the name of the place's owner) and checking if they are equal. However, this way of doing it is useful, as it will work correctly in any place. For instance, you don't need to edit the script before giving it to a friend, as it will adapt to the place. It uses the [[CreatorId (Property)|CreatorId]] property of the [[RBX.lua.DataModel (Object)|DataModel]] and compares it to the player's <abbr title="identifier">id</abbr>. If they are equal, then the player is the owner. Therefore, it works in any place and you don't even need to change your name to the name of your friend when giving it to him.
Here is how you apply this restriction:
<code lua>
game.CreatorId == player.userId
</code>
====Being the Friend of a Certain User====
What if you want all your friends to be able to go through the door? Well, the admins made a method just for that! It is the [[IsFriendsWith (Method)|IsFriendsWith]] method of the Player object. It has one argument, called <var>userId</var>, which is the <abbr title="identifier">id</abbr> of an user. It could be interesting to send game.CreatorId to this method as the argument, if you want to allow all the friends of the creator.
This restriction is simple to apply:
<code lua>
player:IsFriendsWith(3365919)
</code>
'''Note: '''there is also an [[IsBestFriendsWith (Method)|IsBestFriendsWith]] method, which does exactly the same thing, except it instead checks if the player is in the user's best friends list.
===Using Restrictions===
Using restrictions is simple: you just put them in the restrictions table. Let's refresh our memory by looking back at where we were in the code:
<code lua>
local door = script.Parent
function open()
-- This function will open the door.
door.CanCollide = false -- Make players able to walk through the door.
for transparency = 0, 1, .1 do
door.Transparency = transparency
wait(.1)
end
end
function close()
-- This function will close the door.
for transparency = 1, 0, -.1 do
door.Transparency = transparency
wait(.1)
end
door.CanCollide = true -- Make players unable to walk through the door.
end
door.Touched:connect(function(part)
if not part.Parent then return end -- We don't want the script to error just because the part has no parent.
local player = game:GetService('Players'):GetPlayerFromCharacter(part.Parent) or game:GetService('Players'):GetPlayerFromCharacter(part.Parent.Parent)
if not player then return end -- If it isn't a character that touched the door, then we ignore it.
local restrictions = {}
for _, restriction in pairs(restrictions) do
if restriction then
open()
delay(4, close)
end
end
end)
</code>
You see the restriction table? Here it is:
<code lua>local restrictions = {}</code>
Well, just add your restrictions in there. Here is an example that will allow the following players through:
*You, the creator.
*Some predefined players.
*All your friends that are also in a certain group (the group with the <abbr title="identifier">id</abbr> 127081).
*Any player that owns the t-shirt with the <abbr title="identifier">id</abbr> 1032571.
Here is the example:
<code lua>
local door = script.Parent
function open()
-- This function will open the door.
door.CanCollide = false -- Make players able to walk through the door.
for transparency = 0, 1, .1 do
door.Transparency = transparency
wait(.1)
end
end
function close()
-- This function will close the door.
for transparency = 1, 0, -.1 do
door.Transparency = transparency
wait(.1)
end
door.CanCollide = true -- Make players unable to walk through the door.
end
door.Touched:connect(function(part)
if not part.Parent then return end -- We don't want the script to error just because the part has no parent.
local player = game:GetService('Players'):GetPlayerFromCharacter(part.Parent) or game:GetService('Players'):GetPlayerFromCharacter(part.Parent.Parent)
if not player then return end -- If it isn't a character that touched the door, then we ignore it.
local restrictions = {
player.Name == "MrDoomBringer";
player.Name == "JulienDethurens";
player.Name == "blocco";
player.Name == "NXTBoy";
player:IsInGroup(127081) and player:IsFriendsWith(game.CreatorId); -- This demonstrates how you can use 'and' to combine two restrictions.
game:GetService('BadgeService'):UserHasBadge(player.userId, 1032571);
game.CreatorId == player.userId;
}
for _, restriction in pairs(restrictions) do
if restriction then
open()
delay(4, close)
end
end
end)
</code>
==Terminology==
This is a list of some of the terms that were used in this article, as well as their definition, in the context of the article.
;restriction
: A boolean expression that evaluates to true or false depending on the value of the <var>player</var> variable.
;asset
: A specific item, usually represented by its <abbr title="identifier">id</abbr>. Can be a t-shirt, a shirt, a pants, a model, a badge, a place, a hat, a gear and many more.
;identifier
: An unique integer representating a specific thing out of many things of the same kind. Often abbreviated to "id".
;terminology
: The vocabulary of technical terms used in a particular field, in this case, the current article.

Latest revision as of 19:39, 22 April 2012