Creating VIP Doors
Creating VIP Doors
Restricted doors, also commonly called VIP doors, 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.
- Being in a specific group.
- Being the creator of the place.
However, you should always think before making a restricted door that restricts access to a certain area. In most cases, there will be a better 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
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. 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
First, we will need to create a script in the door. Hover over the door part in the Explorer and click on the plus icon , then click on
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. This script will call the variable door, but you can call it whatever you want.
local door = script.Parent
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, as you can see here:
We will use a loop to make the door fade, and we will use its
BasePart/Transparency property to change its opacity. Then, finally, we will change the door’s
BasePart/CanCollide property to allow players to walk through it.
local doorOpen = false local function open() if doorOpen then -- The door is already open, we don't need to open it again! return end doorOpen = true door.CanCollide = false -- Make players able to walk through the door. for transparency = 0, 1, 0.1 do door.Transparency = transparency wait(.1) end end
If there is an open function, there is of course also a close function. The close function does exactly the opposite of what the open function does!
local function close() for transparency = 1, 0, -0.1 do door.Transparency = transparency wait(.1) end doorOpen = false door.CanCollide = true -- Make players unable to walk through the door. end
You probably want the door to open when it is touched, and, therefore, it should use the
BasePart/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.
local function onTouched(hit) local character = hit.Parent if character then local player = game.Players:GetPlayerFromCharacter(character) if player and not doorOpen then open() wait(3) close() end end end
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!
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 separate all the restrictions by the or keyword. All the restrictions will be under the form of boolean expressions. If any of these boolean expressions is true, then the door will open.
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:
player.Name == "PlayerName"
Being in a certain group
Checking if a player is in a certain group is easy using
The IsInGroup method accepts one argument, which is a group’s id. If the player is in the group with that id, the method will return true. Otherwise, it will return false.
Here is an example of applying this restriction (note: you can change 8 to the id of any group):
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
DataModel/CreatorId and compares it to the player’s id. 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:
game.CreatorId == player.UserId
Being the friend of a certain user
What if you want all your friends to be able to go through the door? To do this, use
Player/IsFriendsWith. It has one argument, called userId, which is the id 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:
Now, let’s add our restrictions into the script! Here is an example that will only allow people in the Roblox group to go through:
local door = script.Parent local doorOpen = false local function open() doorOpen = true door.CanCollide = false -- Make players able to walk through the door. for transparency = 0, 1, 0.1 do door.Transparency = transparency wait(.1) end end local function close() for transparency = 1, 0, -0.1 do door.Transparency = transparency wait(.1) end doorOpen = false door.CanCollide = true -- Make players unable to walk through the door. end local function onTouched(hit) local character = hit.Parent if character then local player = game.Players:GetPlayerFromCharacter(character) if player then if player:IsInGroup(7) then -- Only open the door if it's closed. if not doorOpen then open() wait(3) close() end elseif doorOpen then -- Intruder! Don't let them get through the door alive. character:BreakJoints() end end end end door.Touched:Connect(onTouched)