Only player door

This shows you how to make a part "open" when a specific player touches it.

by SintzTutchz

Author Avatar

Do you know tycoon doors that only a specific player can pass through it? This is the idea: make a specific player pass through the part.

You can make designs for it if you want. Everything you have to do is put this inside the part that will be opened.

This isn't a tycoon door;

Other players can pass through it if opened;

If it isn't working, check if you wrote correctly the player's Name/ID

Latest update: Trying to fix an issue I saw in the reviews (this will be in the topic "Some things people said and I'll answer") [05/22/2021]

Let's start

So first, you need to get or create your part that will be the door. You can name it whatever you want, but I'll name mine "Door"(the name wont be used in the script). After it you create a script inside it. Now you might ask: "How am I doing the door open?". It's pretty simple: you just need to create an event to detect when player touches it. Then, it starts:

script.Parent.Touched:Connect(function(hit)

end)

Perfect. Now it is checking if the player has touched the part. If you run, nothing happens. How do we check if the player whose touched is the one that I want? For this case, we are using the if.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent.Name == "" then --Put the player's name inside the quotes

    end
end)

Now it is checking if the player's name is the one that you've chosen. If you run, nothing happens again. "But we have detected if the player touched the part!"; Yes, we do, but we didn't tell it what should happen now. For this case, we need to make some customizations on our door. You can choose some of them for whatever you want, but not all of them. I'll write what you can change:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent.Name == "" then
        script.Parent.CanCollide = false --Don't change.
        script.Parent.Transparency = 0.7 --You can change.
        wait(1.4) --You can change.
        script.Parent.CanCollide = true --Don't change
        script.Parent.Transparency = 0 --You can change.
    end
end)

So, what is happening here? When we used CanCollide for the first time, we changed the Door property so it can't be collided (players can pass through it), then when we used the second time, we chaned it to can be collided (players can't pass through it). When we used Transparency, we are doing what the names supposes: change the transaprency of the Door. If we set it to 1 the Door will be invisible, and the lower it is, more visible it becomes. (Remember that 0 is the lowest and 1 the highest)

Done! Now you have a door that will open when this player touches it. Again, other players can pass it if it's opened, and that's why I put 1.4 seconds to it close again.

Using player's ID to open the Part

Ok, to make this it's the same begin: create a Part/Model that will be your door > Insert a Script inside the Part that will be opened. Now that you have the Part/Model created, we will start as the same way: creating the Touched event connected to a function.

script.Parent.Touched:Connect(function(hit)

end)

"Oh, ok. But if I remember correctly, you said it will do nothing, right?"; Yes, buddy. If you skipped the begin of the tutorial, I'll explain. Else, you can go to the next paragraph if you want to. Here's the explanation: You created the Touchedevent, but didn't tell the script what to do next. Inside it to test, add this: print("Touched"), and touch the part. If everything was made correctly, you saw Touched in the output. Else, check again if what you wrote is correct. You can remove it if worked, but you can leave it there if wanted. "Sintz, what do we do to check if the player that triggered the event has the ID I want?"; There is a function called GetPlayerFromCharacter(). We're going to use it to get the Player. "I saw that if we write 'game.Players.LocalPlayer' you can get it! Why don't we use it? It's sooo simpler."; We can't use it. You can't get the LocalPlayer from a Script, only LocalScript. Let's do the other piece of code:

script.Parent.Touched:Connect(function(hit)
    local LocalPlayer = game.Players:GetPlayerFromCharacter(hit.Parent) --Assign it to a variable, so it's easier
end)

Remember: the variable name can be any, it won't change anything, but make it a clear and easy to read name, so you remember what it does

So here, we assigned the player to hit.Parent. Remember that: hit is what touched and its Parent is, well, the "dad" of the Instance. "Great, now I just copy the rest and done! Thanks, Sintz!"; Where are you going? We're not done yet, and don't simply copy the code, it's not the best option to learn. If you run and your part is colliding with something else, there will be an error. "Wait, your tutorial makes my code error?"; Until now, if you run and touch it, it'll error, but we'll fix it right now. So if the issue is that it's not everything that will find a player by its Parent, we need to check if the Instance has a Humanoid as child.


script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local LocalPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
    end
end)

"Sintz, what is FindFirstChild and what it does?"; I'm not telling a lot about it, but basically it can return 2 values: true or nil. If the given Instance has a child with the given name, it returns true, else if it doesn't, returns nil. In our case, we need the character to get the player, and we did something to just define player if hithas a Humanoid. Now if you run, the code will not error. We need now check if the player's ID is the one you chose, and if the player exists. "We are using if statements, right?";Yes! You guesses right! "Also, how so 'check id player exists'?"; Well, you perhaps know that NPCs have Humanoid. Our code allows anything with Humanoid runs and when we defined the player, it was given the value nilbecause the NPC is not a player, and the code will throw an error because UserId is not a valid member of nil. Got it? "How to do it?"; I'll teach you now.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local LocalPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
        if LocalPlayer then --Checks if the player exists. If doesn't, it won't open
            if LocalPlayer.UserId == 123456 then --Change 123456 for the ID you want

            end
        end
    end
end)

Great, the script is now checking if the given ID is from the player that touched the Part. If it is, then something will happen, if it isn't it won't happen. It also checks if LocalPlayer exists, so it won't error when using UserId. "Wait, why?"; NPCs have Humanoid, so if they touch the part they will trigger the first if statement. They aren't players, so UserId errors with them. Now let's tell the code what to do if the ID is right. In our case, we want it to open our Door, so we are using the CanCollide property (mainly), and to be sure it opened, the Transparency property. CanCollide will make the Part be able to go through or not. Transparency is the much we can see through the Part. The transparency property can be changed, but not the CanCollide. I'll write better in the sample:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local LocalPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
        if LocalPlayer then
            if LocalPlayer.UserId == 123456 then --Change 123456 for the ID you want
                script.Parent.CanCollide = false --Don't change it. If you do so it'll not work
                script.Parent.Transparency = 0.6 --You can change it
                wait(1.4) --You can change it. It'll define the ammount of time before the door close
                script.Parent.CanCollide = true --Don't change it
                script.Parent.Transparency = 0        --It's ok change it
            end
        end
    end
end)

Wow! We did it! We finally made this door work! Now if everything was made correctly, the door will open if the ID is the same as the given one. Remember: if you write the ID incorrectly the door will not open, only if the ID match with the given one

Sorry for the long time without updating it, when I was writing the update Roblox crashed (three times) and I lost the update. It took me time to rewrite things. I hope you understand it :)

Making the code look a little better

So, if you check in both codes we have used script.Parent many times during the code. This is not good practice honestly, and that's why I came up with this new topic. It's a really short one with the idea to make the code better to read.

"Sintz, how we should do that?"; Perhaps if you're a more experienced scripter this question was not made and you have already done it, but if you don't know the answer we're gonna use variables.

If you don't know what variables are, check out Lua Learning lessons. There you can find what it is and how to use.

So the code goes:

local OnlyPlayerDoor = script.Parent --The name doesn't need to be this. Change it for whatever you want.


--From here, the code is the same, the only thing that changes is that you'll substitute all ´script.Parent´ with the variable's name. I'll write the one that uses the name just as an example.

OnlyPlayerDoor.Touched:Connect(function(hit)
    if hit.Parent.Name == "CoolName" then
        OnlyPlayerDoor.CanCollide = false
        OnlyPlayerDoor.Transparency = 0.7
        wait(1.4)
        OnlyPlayerDoor.CanCollide = true
        OnlyPlayerDoor.Transparency = 0
    end
end)

You can do this with both, ID and Name.

Some things people said and I'll anwer

"Can I use player's ID?"; Yes!! (the comment was made before the update about UserId to open the door)

"Can I have many names inside the double quotes?"; Not inside the same, but if you write the name, put or and another name. Example:

--The begin of the code
    if hit.Parent.Name == "Name1" or hit.Parent.Name == "Name2" then --Yes, you need to write "hit.Parent.Name" again.
--Code continues

You can also scroll down to see another way to do it.

"My code doesn't work, and I wrote my name correctly!"; Be sure of: the script is inside the part that will open, not the model or another part or ServerScriptService; you wrote the code correctly; you didn't messed up while writing the parameter and using it (changing a letter or something like that).

"I heard that I can type 'Bob' inside the quotes. Can I type 'Bob'?"; Yes, you do, but you need to keep in mind that what comes inside the double quotes is the player's name! If you type Bob, a player with username called Bob will pass.

"I saw the example of 2 names, but can I type more?" Yes, but you'll need to copy and paste the line of the code again. Example:

if hit.Parent.Name == "Name1" or hit.Parent.Name == "Name2"  or hit.Parent.Name == "Name3" or hit.Parent.Name == "Name4" then -- You can type how many names you wanna if you do it like this

Or you can do it in a simpler way. You'll use tables

--The script needs to be inside the part that will open
local Names = {"Name1","Name2"} --Put how many names you wanna, but keep in mind that you'll still use the double quotes
script.Parent.Touched:Connect(function(hit)
    for _,name in pairs(Names) do --This will iterate through all the names you've put inside the table.
        if hit.Parent.Name == name then
            script.Parent.CanCollide = false 
            script.Parent.Transparency = 0.7 
            wait(1.4) --You can change.
            script.Parent.CanCollide = true 
            script.Parent.Transparency = 0
        end
    end
end)
--This also works for the IDs
--The only difference is the *for* loop

I'm not going in a deep explanation about for loops, but _,names is like doing _,v. I used underscore instead of i because I do not need the index. For any further help about for loops, check out about loops (I think there is a tutorial here about it)

"Everything else worked, it just won't become solid again"; Ok, if it isn't becoming solid, then check if you wrote everything after the wait(1.4) correctly. This will fix.

"I followed everything correctly but it doesn't work"; If you are 100% sure it is not working even if the code is identical, check the following: 1. The script is inside of the part that you're going to 'open'; 2. It is a Script and not a LocalScript; 3. If you're not using script.Parent to get the part, be sure you wrote the right path (e.g. game:GetService("Workspace").MyCoolPart). If it doesn't fix then check NotAPorgu's debugging tutorial and check what is going on. I hope that helps you.

That's it for now. I'll be answering your questions always. Feel free to ask me any issues you have, alright?

This is it

Thank you for reading this. Hope you learnt how to make it :)