I am trying to make a Door that only me can walk through. Any idea on the script? Would it be Local or Regular? And then in the answer please paste the script! Thanks!
If you would like it to be perfectly efficient then it must be a local part (place it inside the camera, the door). In this there will be a script which has a function which triggers when the part is touched. It may look something like this:
script.Parent.Touched:connect(function(touch) if touch.Parent:FindFirstChild("Humanoid") ~= nil then -- Checks it's a player if touch.Parent.Name == "YOURNAMEHERE" then -- Checks if it's you script.Parent.Transparency = 0.6 script.Parent.CanCollide = false wait(3) script.Parent.Transparency = 0 script.Parent.CanCollide = true end end end)
You would want a Script, and you would want to put it in the Door brick, not a model if it is part of a model.
Now let me walk you through it.
The first thing we want to do is create a function to detect when the door is touched, like so:
function onTouch(hit) end
Now, we want the door to open when somebody touches it who is you, and remain closed when somebody who is not you touches it. Unfortunately, this function will be used any time ANY part hits the door, not necessarily just a person. So, we need to make sure the person touching the door is a person, which can be done like so:
function onTouch(hit) if hit.Parent.Humanoid ~= nil then end end
What the above code does is detect if the parent of the part that hit it (in the case of a Player, the model of the Player that moves in the game, called the Character) has a Humanoid within it, a part that is (usually) only found in Players. Another way to do it would be like so and would not be fooled by Models with Humanoids in them:
function onTouch(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then end end
This simply checks if there is a Player (in Players) whose name is the same as the Parent of the part that hit the door. I, personally, prefer the second method, so I will continue to use it in this tutorial.
The next thing we want to do is make sure the person hitting the door is still alive. Why? If somebody kills you, or you reset, and you touch the door, anybody else will be able to walk through. So, we add a little piece of code that checks whether the Character's Humanoid has a health greater than 0.
function onTouch(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then if hit.Parent.Humanoid.Health > 0 then end end end
Now that we've done that, we can get to the actual, important piece of the code: opening the door. Here, we want to check the username of the Player (in Players) and make sure that this person who touched your door is you. then we want to open the door. Since I will be using this Player again, I will make him/her into a variable.
function onTouch(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then if hit.Parent.Humanoid.Health > 0 then player = game.Players:GetPlayerFromCharacter(hit.Parent) if player.Name == "iiShadowTheif" then script.Parent.Transparency = 0.7 script.Parent.CanCollide = false end end end end
And that will make the door open for you! Make sure to put it in the door brick.
If you want to kill the person who touches the door if they're not you, you can add this tiny little piece:
function onTouch(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then if hit.Parent.Humanoid.Health > 0 then player = game.Players:GetPlayerFromCharacter(hit.Parent) if player.Name == "iiShadowTheif" then script.Parent.Transparency = 0.7 script.Parent.CanCollide = false else player.Character:BreakJoints() end end end end
BreakJoints() is basically a method that kills a character.
Thanks for reading all of this (if you actually did) and I hope it helped you!
~Kaamchor
script.Parent.Touched:connect(function(hit) if hit.Parent and Game.Players:GetPlayerFromCharacter(hit.Parent) then player=Game.Players:GetPlayerFromCharacter(hit.Parent) if player.userId==Game.CreatorId then script.Parent.CanCollide=false wait(3) script.Parent.CanCollide=true else script.Parent.CanCollide=true end end end)
I haven't tested this, but it should work. Tell me if you encounter any problems.
First, use correct grammar, It's not "me" It's "I", second, just get one from the catalog, that's what I did.