Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do I make a door that only I can go through??

Asked by 11 years ago

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!

4 answers

Log in to vote
0
Answered by
TomsGames 225 Moderation Voter
11 years ago

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:

01script.Parent.Touched:connect(function(touch)
02if touch.Parent:FindFirstChild("Humanoid") ~= nil then -- Checks it's a player
03if touch.Parent.Name == "YOURNAMEHERE" then -- Checks if it's you
04script.Parent.Transparency = 0.6
05script.Parent.CanCollide = false
06wait(3)
07script.Parent.Transparency = 0
08script.Parent.CanCollide = true
09end
10end
11end)
0
Why are you looking for a Humanoid rather than using GetPlayerFromCharacter()? What if there's a bot that has his name? TheGuyWithAShortName 673 — 11y
0
If there's a bot that has his name then it can walk through the door. Lesson: Don't have a bot with his name. TomsGames 225 — 11y
Ad
Log in to vote
1
Answered by
wrenzh 65
11 years ago

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:

1function onTouch(hit)
2 
3end

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:

1function onTouch(hit)
2    if hit.Parent.Humanoid ~= nil then
3 
4    end
5end

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:

1function onTouch(hit)
2    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
3 
4    end
5end

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.

1function onTouch(hit)
2    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
3        if hit.Parent.Humanoid.Health > 0 then
4 
5        end
6    end
7end

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.

01function onTouch(hit)
02    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
03        if hit.Parent.Humanoid.Health > 0 then
04            player = game.Players:GetPlayerFromCharacter(hit.Parent)
05            if player.Name == "iiShadowTheif" then
06                script.Parent.Transparency = 0.7   
07                script.Parent.CanCollide = false
08            end
09        end
10    end
11end

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:

01function onTouch(hit)
02    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
03        if hit.Parent.Humanoid.Health > 0 then
04            player = game.Players:GetPlayerFromCharacter(hit.Parent)
05            if player.Name == "iiShadowTheif" then
06                script.Parent.Transparency = 0.7   
07                script.Parent.CanCollide = false
08            else
09                player.Character:BreakJoints()
10            end
11        end
12    end
13end

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

0
I feel like this was a bit lengthy and went into too much detail. Sorry if I bored you. wrenzh 65 — 11y
Log in to vote
0
Answered by 11 years ago
01script.Parent.Touched:connect(function(hit)
02if hit.Parent and Game.Players:GetPlayerFromCharacter(hit.Parent) then
03player=Game.Players:GetPlayerFromCharacter(hit.Parent)
04if player.userId==Game.CreatorId then
05script.Parent.CanCollide=false
06wait(3)
07script.Parent.CanCollide=true
08else script.Parent.CanCollide=true
09end
10end
11end)

I haven't tested this, but it should work. Tell me if you encounter any problems.

0
I'm guessing in line 4 you change CreatorId with my actual profile ID? iiShadster 0 — 11y
0
No, it should work just like that (assuming the game is yours). TheGuyWithAShortName 673 — 11y
Log in to vote
-3
Answered by 11 years ago

First, use correct grammar, It's not "me" It's "I", second, just get one from the catalog, that's what I did.

0
I'm sorry for my grammer. ;/ And I am trying to make one not get one from the catalog iiShadster 0 — 11y

Answer this question