How to open a door on key

Hello everyone. I am trying to figure out how to open my door by hovering my mouse over and pressing the key to open it instead of having to click it.

image

This is what the door looks like, and here are the two scripts inside. Any help is greatly appreciated.

	script.Parent.Parent.Parent.Motor.DesiredAngle = 1.5
	wait(4.5)
	script.Parent.Parent.Parent.Motor.DesiredAngle = 0
end

script.Parent.CLICKD1.MouseClick:connect(onClick) ```




``` Sound = script.Parent.Sound
Sound2 = script.Parent.Sound2

function onClicked()
Sound:Play()
wait(2.5)
Sound2:Play()
end

script.Parent.CLICKD1.MouseClick:connect(onClicked) ```

The way I achieved a door opening on click was with Hinge Constraints.
If you set up the hinge constraints, then when you click, you just have to change the Orientation on the Y axis.

Here is the script I used:

local speed = 4
local increment = 0
local direction = false -- false = left, true = right
local debounce = false
local door = script.Parent

script.Parent.ClickDetector.MouseClick:Connect(function()
	if debounce == false then 
	    debounce = true
        increment = direction and 2 or -2

	    for i = 1,10 do
		    wait()
		    door.Orientation = door.Orientation + Vector3.new(0,increment,0) * speed
	    end	
        direction = not direction		       
        debounce = false
    end
end)

Feel free to use my script and if you have questions let me know. :slight_smile:

2 Likes

That looks very neat but I am trying to make it open on key. Like I hover my mouse over the door, a UI pops up with the key “F” and then I press it and the door will open.

Then you just need to listen for User Input Service if the UI is visible.
I have a code example of using it if you’d like me to show it.

https://wiki.roblox.com/index.php?title=API:Class/UserInputService

1 Like

Yes please I would love to see the code example. I am bad at setting things up.

Here is a example of it being used in a UI.

-- LocalScript in your UI
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenDoor = ReplicatedStorage:WaitForChild("OpenDoor") -- RemoteEvent
local DialogController = script.Parent.Parent.DialogController
local Choices = script.Parent.Parent.Choices
local DialogQuestion = script.Parent.Parent.DialogQuestion

UserInputService.InputBegan:Connect(function(InputObject, Processed)
	if not Processed then
        if InputObject.KeyCode == Enum.KeyCode.F then
                OpenDoor:FireServer()
	    end
    end
end)
--This would be inside your door, a ServerScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenDoor = ReplicatedStorage:WaitForChild("OpenDoor") -- RemoteEvent
local speed = 4
local increment = 0
local direction = false -- false = left, true = right
local debounce = false
local door = script.Parent

OpenDoor.OnServerEvent:Connect(function()
    if debounce == false then 
	    debounce = true
        increment = direction and 2 or -2

	    for i = 1,10 do
		    wait()
		    door.Orientation = door.Orientation + Vector3.new(0,increment,0) * speed
	    end	
        direction = not direction		       
        debounce = false
    end
end)
2 Likes

And I would need to make it require a remove event that I would put in replicatedstorage right?

1 Like

Yes, you would need to create a RemoteEvent, I edited my reply to have both the Client Side Script and the Server Side Script.
I also went ahead and reformatted your SoundPlaying Script. (nothing wrong with how you had it, but this is a little more efficient. :slight_smile:

local Sound = script.Parent.Sound
local Sound2 = script.Parent.Sound2

script.Parent.CLICKD1.MouseClick:Connect(function()
Sound:Play()
wait(2.5)
Sound2:Play()
end)

Hopefully this helps. :smiley:

1 Like

Thank you a ton!

1 Like

You’re welcome!! If you have any more questions, let me know. :slight_smile:

1 Like

Quick question, should the UI be a screen UI or billboard.

What I used it for was a screen… So I’m not sure if a Billboard would work the same or not.

1 Like

I am having a bit of trouble setting up the screen UI. Do I need to have any frames?

That shouldn’t matter, you should just need to make sure that it is a LocalScript and Enabled.

1 Like

This is what I currently have set up:
image

Inside my door

image

RemoteEvent and UI

oh I’d create a TextLabel that says “Press ‘F’ to Open Door.”
Make sure you have it configured to “F” in your Localscript.

1 Like

Would I need to put something in the localscript so when your mouse hovers over the door the textlabel becomes visible?

--ServerScript Place this in your door with the clickdetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.ClickDetector.MouseHoverEnter:connect(function(Character)
    local Player = game.Players:GetPlayerFromCharacter(Character
    local UI = ReplicatedStorage:WaitForChild("UINAMEHERE"):Clone()
    UI.Parent = Player.PlayerGui
end)

Put your UI Into ReplicatedStorage and name it something, and change in the script where it says “UINAMEHERE” to whatever your UI is.

(I think I used “GetPlayerFromCharacter” properly, if you get errors tell screen shot the output.)

1 Like

img

LocalScript
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenDoor = ReplicatedStorage:WaitForChild("OpenDoor") -- RemoteEvent

UserInputService.InputBegan:Connect(function(InputObject, Processed)
	if not Processed then
        if InputObject.KeyCode == Enum.KeyCode.F then
                OpenDoor:FireServer()
	    end
    end
end)
ServerScript
--ServerScript Place this in your door with the clickdetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.ClickDetector.MouseHoverEnter:connect(function(Player)
    local UI = ReplicatedStorage:WaitForChild("Interaction"):Clone()
    UI.Parent = Player.PlayerGui
end)

local OpenDoor = ReplicatedStorage:WaitForChild("OpenDoor") -- RemoteEvent
local speed = 4
local increment = 0
local direction = false -- false = left, true = right
local debounce = false
local door = script.Parent

OpenDoor.OnServerEvent:Connect(function()
    if debounce == false then 
	print("boo")
	    debounce = true
        increment = direction and 2 or -2

	    for i = 1,10 do
		    wait()
		    door.Orientation = door.Orientation + Vector3.new(0,increment,0) * speed
	    end	
        direction = not direction		       
        debounce = false
    end
end)

Change out your scripts to what I just posted here. :slight_smile:
I tested them and they all work how they should.

2 Likes

The door opens now which is awesome but the screenui doesn’t appear when I hover my mouse and it’ll let me open the door from any distance