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 10 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
10 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:

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)
0
Why are you looking for a Humanoid rather than using GetPlayerFromCharacter()? What if there's a bot that has his name? TheGuyWithAShortName 673 — 10y
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 — 10y
Ad
Log in to vote
1
Answered by
wrenzh 65
10 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:

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

0
I feel like this was a bit lengthy and went into too much detail. Sorry if I bored you. wrenzh 65 — 10y
Log in to vote
0
Answered by 10 years ago
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.

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

Answer this question