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

How does one create a door using .KeyDown?

Asked by
Xduel 211 Moderation Voter
10 years ago

I understand this question is a bit specific, but I can assure I will apply an answer to other work. So for this situation, I would like to make a door that when walking up to it, if the player is in a certain range of the door, upon the press of a certain key will it open. I attempted this, and upon failed attempts and a crashed ROBLOX (Which is why I have no code to display) I am open for any suggestion on how to achieve this. All answers appreciated even if unsure, thank you. ~ Xduel

1 answer

Log in to vote
4
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

First let's make a table of the people with the ability to open the door..

Permission = {"Xduel","Goulstem","Another Person"}

Next we'll get the LocalPlayer's mouse, and do a KeyDown event, while checking if their allowed to do the command..

mouse = game.Players.LocalPlayer:GetMouse()

mouse.KeyDown:connect(function(key)
    for i,v in pairs(Permission) do
        if v == game.Players.LocalPlayer.Name then

Now we check if their in the correct vicinity to open the door..

distance = --Wanted minimum distance from door here
if (game.Players.LocalPlayer.Character.Torso.Position - Door.Position).magnitude <= distance then

And then we check if they key is the correct key..

keyy = "Wanted Key To Press Here"
if (key:lower()) == keyy then

And finally open the door..

Door.Transparency = .5
Door.CanCollide = false
wait(3)
Door.Transparency = 0
Door.CanCollide = true

Final Product

Permission = {"Xduel","Goulstem","Another Person"}
mouse = game.Players.LocalPlayer:GetMouse()

mouse.KeyDown:connect(function(key)
    for i,v in pairs(Permission) do
        if v == game.Players.LocalPlayer.Name then
            distance = --Wanted minimum distance from door here
            if (game.Players.LocalPlayer.Character.Torso.Position - Door.Position).magnitude <= distance then
                keyy = "Wanted Key To Press Here"
                if (key:lower()) == keyy then   
                    Door.Transparency = .5
                    Door.CanCollide = false
                    wait(3)
                    Door.Transparency = 0
                    Door.CanCollide = true
                end
            end
        end
    end
end)

Hope I Helped

+1

Ad

Answer this question