Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Transforming a clickable door into a keybind one?

I have been trying to make my door button a keybind (Pressing E on it) instead of clicking on the button, which makes it difficult as you have to put your tool down to be able to open it. If anyone could help me, I would appreciate it. Thanks!

Here's my current script.

local isopen = script.Parent.IsOpen
local idle = true
local button1 = script.Parent.Button1
local button2 = script.Parent.Button2
local click1 = button1.ClickDetector
local click2 = button2.ClickDetector
function active(silent)
	if idle and not script.Parent.Locked.Value then
		idle = false
		local count = 0.070
		if not isopen.Value then
			script.Parent.Door1.CanCollide = false
			script.Parent.Door2.CanCollide = false
			if not silent then
				script.Parent.Door1["Open" .. math.random(1,3)]:Play()
			end
			for i=1,200 do
				wait()
				script.Parent.Door1.CFrame = script.Parent.Door1.CFrame * CFrame.new(count * 1.0,0,0)
				script.Parent.Door2.CFrame = script.Parent.Door2.CFrame * CFrame.new(count,0,0)
				if i < 18 then
					count = count * 1.05
				else
					count = count / 1.05
				end
			end
			isopen.Value = true
		else
			script.Parent.Door1.CanCollide = true
			script.Parent.Door2.CanCollide = true
			if not silent then
				script.Parent.Door1["Close" .. math.random(1,3)]:Play()
			end
			for i=1,200 do
				wait()
				script.Parent.Door1.CFrame = script.Parent.Door1.CFrame * CFrame.new(-count * 1.0,0,0)
				script.Parent.Door2.CFrame = script.Parent.Door2.CFrame * CFrame.new(-count,0,0)
				if i < 18 then
					count = count * 1.05
				else
					count = count / 1.05
				end
			end
			isopen.Value = false
		end
		wait()
		idle = true
	end
end

button1.ClickDetector.MouseClick:connect(function()
	button1.Push:Play()
	active()
end)
button2.ClickDetector.MouseClick:connect(function()
	button2.Push:Play()
	active()
end)

1 answer

To be able to do this you will need to use a local script to detect if a button has been pressed using UserInputService. Then to communicate with the server that a person has pressed a button, you will have to use a RemoteEvent in order to send that information, and then the server script will do the task.

I just put this together really quickly for you.

Put this script as a LocalScript in StartPlayer>StarterPlayerScripts

--- Put this in a localscript
--- Make a RemoteEvent and put it under ReplicatedStorage
local Players = game:FindService("Players")
local ReplicatedStorage = game:FindService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Event = ReplicatedStorage.DoorEvent

local LocalPlayer = Players.LocalPlayer
repeat wait() until LocalPlayer.Character

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessed then
        if input.KeyCode == Enum.KeyCode.E then
            Event:FireServer()
        end
    end
end)

Make sure to make a RemoteEvent and call it "DoorEvent" then put it under ReplicatedStorage

Then, instead of the buttons click listeners, add this to your server script

local ReplicatedStorage = game:FindService("ReplicatedStorage")
local Door = script.Parent 
local Event = ReplicatedStorage.DoorEvent
local doorRange = 20 -- studs as a radius of distance to be able to open the door

Event.OnServerEvent:Connect(function(player, ...)
	local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart and (humanoidRootPart.Position - Door.Position).magnitude <= doorRange then
		active()
	end
end)

Note: instead of using a for loop to tween the door, a more efficient and easier way to do that is to use TweenService

Hope this helps, please let me know if you have further questions

This discussion is preserved as it was. New answers, comments, and votes are closed.