generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: How to open a door by pressing a key on the keyboard.

Previous Thread :: Next Thread 
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
23 Jul 2014 11:39 PM
Title says it all mostly... I have been searching for tutorials for ever and not one has explained how to make a door slide open when you click a key on the keyboard.

Does anyone have any suggestions on how this can be achieved?



I have created a script and put it in the Player Starter Pack... not quite sure if you need this to be able to press a key for an event to occur or not.

SCRIPT:
Player = script.Parent.Parent
mouse = Player:GetMouse()

function onKeyDown(key)
key = key:lower()

if key == "e" then




end

end
Report Abuse
nomer888 is not online. nomer888
Joined: 13 Feb 2010
Total Posts: 551
23 Jul 2014 11:41 PM
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(k)
if k == "e" then
--open the door
end
end)
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
23 Jul 2014 11:44 PM
put nomer's code in a localscript wherever you want
Report Abuse
TwistedFaker is not online. TwistedFaker
Joined: 08 May 2014
Total Posts: 31
23 Jul 2014 11:44 PM
No^

Only in the character, playergui or playerbackpack
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
23 Jul 2014 11:50 PM
Do I need to add a separate script in the sliding door itself to open when I get close to it and press the key "e"?
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
23 Jul 2014 11:54 PM
Oh by the way... play the game "Darkness" that is the type of door I am trying to create
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
24 Jul 2014 01:08 AM
Alright this is the code I have came up with which I have put in the button that makes the door open:

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(k)
if k == "e" then

for i = 1,90 do
wait()
script.Parent.Parent.Door.CFrame = script.Parent.Parent.Door.CFrame * CFrame.new(0.1, 0, 0) * CFrame.fromEulerAnglesXYZ(0, 0, 0)
end
script.Value.Value = true
script.Parent.Parent.BrickColor = BrickColor.new("Bright red")
else
for i = 1,90 do
wait()
script.Parent.Parent.Door.CFrame = script.Parent.Parent.Door.CFrame * CFrame.new(-0.1, 0, 0) * CFrame.fromEulerAnglesXYZ(0, 0, 0)
end
script.Value.Value = false
script.Parent.Parent.BrickColor = BrickColor.new("Lime green")

end
end )


A FEW PROBLEMS:
- Slides to slowly
- Pressing "e" doesn't open the door
- Color of block doesn't change to red as I press it


Any suggestions?
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
24 Jul 2014 08:56 AM
How do you know it goes to slowly if it doesn't even go?
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
24 Jul 2014 12:04 PM
@Kodran I made it open using a button which I click to open it and it opens way to slowly...

UPDATE:
I have gotten one door to open using this code; however, when I make duplicates of the doors and try to open the duplicates they do not open. What am I doing wrong people?!?! This Local Script is in the players starter pack:

Permission = {"Xduel","Goulstem","Player1"}
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 = 10 --Wanted minimum distance from door here
print("Press E To Open")
if (game.Players.LocalPlayer.Character.Torso.Position - Workspace.SlidingDoor.Door.Position).magnitude <= distance then
keyy = "e"
if (key:lower()) == keyy then
Workspace.SlidingDoor.Door.Transparency = .7
Workspace.SlidingDoor.Door.CanCollide = false
wait(3)
Workspace.SlidingDoor.Door.Transparency = 0
Workspace.SlidingDoor.Door.CanCollide = true
end
end
end
end
end)



Could it be that I just need to put this script in every door I want to open when I get near it and press "e"?
Please help people we almost got it!
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
24 Jul 2014 12:07 PM
Oh, one other thing... I wanted it to say when you get close to the door "Press E To Open"

I tried to do this using this code (taken from the code above):
print("Press E To Open")

This code does not work however....


So what am I doing wrong with all these things?
Report Abuse
Immacularity is not online. Immacularity
Joined: 21 Jun 2014
Total Posts: 419
24 Jul 2014 12:30 PM
LocalScript.
This is a ridiculously painful way to do it and this also took me fifteen minutes, but here. Put your doors in a model in Workspace named "Doors".

mouse=game.Players.LocalPlayer:GetMouse()
function checkDoorDistance(torso,doors,dist)
for i,v in pairs(doors) do
if (v.Position-torso.Position).magnitude<=dist then
return true
end
end
end

function findNearestDoor(torso,doors)
mags={}
for i=1,#doors do
table.insert(mags,#mags+1,(#doors[i].Position-torso.Position).magnitude)
end
return math.min(mags)
end

while wait(0.5) do
if checkDoorDistance(game.Players.LocalPLayer.Character.Torso ,Workspace.Doors:GetChildren()) then
--create a gui, I'm too lazy
end

mouse.KeyDown:connect(function(key)
if key=="e" and checkDoorDistance() then
door=findNearestDoor(game.Players.LocalPlayer.Character.Torso,Workspace.Doors:GetChildren())
--open door, (door is defined as door) I'm too lazy to write that
end
end)
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
24 Jul 2014 01:19 PM
Ok so I have this script the Starter Pack with a model in the Workspace labeled "Doors" and inside this model there are four doors all labeled "Door". I tested it out but it won't work. Did I name the parts wrong?

mouse=game.Players.LocalPlayer:GetMouse()
function checkDoorDistance(torso,doors,dist)
for i,v in pairs(doors) do
if (v.Position-torso.Position).magnitude<=dist then
return true
end
end
end

function findNearestDoor(torso,doors)
mags={}
for i=1,#doors do
table.insert(mags,#mags+1,(#doors[i].Position-torso.Position).magnitude)
end
return math.min(mags)
end

while wait(0.5) do
if checkDoorDistance(game.Players.LocalPLayer.Character.Torso ,Workspace.Doors:GetChildren()) then
--create a gui, I'm too lazy
end

mouse.KeyDown:connect(function(key)
if key=="e" and checkDoorDistance() then
door=findNearestDoor(game.Players.LocalPlayer.Character.Torso,Workspace.Doors:GetChildren())
Workspace.Doors.Door.Transparency = .7
Workspace.Doors.Door.CanCollide = false
wait(3)
Workspace.Doors.Door.Transparency = 0
Workspace.Doors.Door.CanCollide = true
end
end)
end
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
24 Jul 2014 01:25 PM
If you reference the same door in every script, of course the same door will open every time!

What you should do is name all your doors differently, so you can refer to different ones in your script.

Or you can judge by distance, which I believe someone already provided an example for that

-I just hit 4k posts, made a lengthy speech lol: http://www.roblox.com/Forum/ShowPost.aspx?PostID=141338105-
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
24 Jul 2014 01:33 PM
This script here works if I put each door in a model named SlidingDoor, SlidingDoor1, Etc... with a new Local Script for each door:

Permission = {"Xduel","Goulstem","Player1"}
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 = 10 --Wanted minimum distance from door here
print("Press E To Open")
if (game.Players.LocalPlayer.Character.Torso.Position - Workspace.SlidingDoor.Door.Position).magnitude <= distance then
keyy = "e"
if (key:lower()) == keyy then
Workspace.SlidingDoor.Door.Transparency = .7
Workspace.SlidingDoor.Door.CanCollide = false
wait(3)
Workspace.SlidingDoor.Door.Transparency = 0
Workspace.SlidingDoor.Door.CanCollide = true
end
end
end
end
end)


I need the "Permissions" to allow any player that plays my game to be allowed to open the door. Right now (as you can see) only "Xduel","Goulstem", and "Player1" have permission
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
24 Jul 2014 07:37 PM
Bump.
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
25 Jul 2014 01:53 AM
Bump 2
Report Abuse
cfiredog is not online. cfiredog
Joined: 30 May 2008
Total Posts: 228
25 Jul 2014 07:13 PM
Come on people I need help...
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image