How do I make door's speed change based on the players input speed

What am I trying to achieve?

I’ve been making a horror game as of late, and I was wondering if there is a possible way to code a door in which when the player activates it (a ProximityPrompt), that the door will open faster or slower depending on the rate at which the player presses the prompt (if the player holds the prompt, the longer it takes the door to open), sort of like what they have in Outlast.


My initial idea:

So far, all I’ve really done is looked into Roblox’s functionality that’s built into the ProximityPrompts, but I’m not sure if using the ProximityPrompt.Trigger and ProximityPrompt.TriggerEnded would be a viable solution.


My initial idea looks something like this (it doesn’t have to work like this):

local prox = script.Parent

local started = false
local s_count
local f_count

local function count(v)
	if v == true then
		print("started")
		started = true
		s_count = time()
	elseif v == false then
		started = false
		f_count = time() - s_count
		print("player stopped hold", s_count, f_count)
	end
	return s_count, f_count
end

prox.Triggered:Connect(function()
	task.spawn(count, true)
end)

prox.TriggerEnded:Connect(function()
	task.spawn(count, false)
end)

Current Idea:

Now I'm trying to make the door open according the the players movements (e.g. when the player has a forward motion, the door will open in that direction, and vice versa)

I feel you could determine it based upon a few factors;

  1. Current Character speed,
  2. Distance from character to door when interacted with it

I know you said “input speed” but if you are “running” or at a “running speed” you could say the doors speed to be faster.

1 Like

Not sure how it’d translate to gameplay, I feel like it wouldn’t feel that good.

I’d do a button for peeking, opening the door a little to get a view inside the room (or hold briefly to open slightly / press once to peek), or you could maybe look through the keyhole?

It would be nice if you could detect how deep the key is pressed (e.g. just enough to reach the actuation point making it open the door slightly, or bottom it out to fully open the door, adjusting speed based on how fast the key is pressed), but it’d be a weird mechanic, since I’ve never seen it in a game. Also not really feasible from what I know of the Roblox API.

Like the poster above mentioned, you could use character speed, if they’re moving and within certain speed, you could open the door faster, as compared to a slower opening while they’re standing still.

Anyway, to answer your question; your method would work, whether you use proximity prompts or not.

1 Like

edited this free model script. here ya go:

local TweenService = game:GetService("TweenService")

local hinge = script.Parent.Doorframe.Hinge
local prompt = script.Parent.Base.ProximityPrompt

local goalOpen = {}
goalOpen.CFrame = hinge.CFrame * CFrame.Angles(0, math.rad(90), 0)

local goalClose = {}
goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)

local inputStartTimes = {}
local normalInputTime = 0.25
local normalOpenTime = 1
local maxOpenTime = 5

prompt.Triggered:Connect(function(player)
	inputStartTimes[player] = time()
end)

prompt.TriggerEnded:Connect(function(player)
	if not inputStartTimes[player] then return end
	
	local elapsedInputTime = time() - inputStartTimes[player]
	inputStartTimes[player] = nil

	-- makes it so that the longer the player pressed, the longer it takes for the door to open till a maximum of maxOpenTime
	local openTime = math.clamp(elapsedInputTime / normalInputTime, normalOpenTime, maxOpenTime)
	
	if prompt.ActionText == "Close" then
		TweenService:Create(hinge, TweenInfo.new(openTime), goalClose):Play()
		prompt.ActionText = "Open"
	else
		TweenService:Create(hinge, TweenInfo.new(openTime), goalOpen):Play()
		prompt.ActionText = "Close"
	end
end)

you can mod ur old script or smth. also here is the entire model:
slowdoor_fastdoor!.rbxm (23.0 KB)
randomg name lol

Honestly might not be the worst idea to have 2 buttons, but it could have too many inputs involved to where it’ll be annoying. But I definitely will tinker with the idea of both multiple inputs and using the characters movement speed

hey there, just wondering if youve taken a look at my response

(sorry for the grammatical and spelling errors i made - i was tired while writing that)

Edit: shouldnt be past tense cuz i still am lol

I’m not sure that this is completely what I’m look for. I want it to interact in real time whereas this will have a delay where the player presses and releases, which could be awkward where you’ll need to hold and the door won’t open until you release. I’m trying to make it where the longer you hold down, the longer it takes the door to open.

sorry if im being stupid, but how else would that work? once they open the door you cant slow it down like time travel

So my thought behind it was you have the hold duration, but when you either complete the trigger or release early, the door will slowly open until its fully open. But now I don’t that’d really work due to the fact that you’d need to open the door fully in order for it to close.

The game that I got the idea from (Outlast) has it where when you interact with the door, when you move forward or backward, the door will move accordingly.

wait im having a stroke rn. isnt that what i did? depending on how long you hold it down it will decide the door opening speed. or was your idea like when you press, the door opens, but as you hold it, the opening speed decreases?

wait where did closing come into play

Well my starting idea was your second option. But now I’m saying I’d need it to move according to the players movement

(Moving forward opens the door, moving backward closes the door)

ah alright. thats completely different :sob::folded_hands:. well that was for nothing. ok so should the player move while opening and closing the door

I think I’ll try it out where when the player interacts (holding interact or locked to player idk) with the door, the door will open based on if they move or not

I’ll post back if I decide whether this idea is really viable or if I choose that it won’t be relevant to the gameplay

I personally never tried doing anything similar, But i would recommend making the proximity prompt’s hold duration a large (not too large) number, And using two DateTime variables to determine the time the player started/stopped holding the proximity prompt to use the difference between the unix timestamps of those two DateTime variables as a factor on how fast the door opens

For this example i will tween the door with a speed based on how short the time between holding and letting go of the proximity prompt is (the lower the time, the higher the speed), But keep in mind this is just to give an idea on how it might work.

local tweenservice = game:GetService("TweenService")
local prox = script.Parent
local start = DateTime.now().UnixTimestampMillis*0.001
local maxtime = prox.HoldDuration 
-- make the maxtime variable large but not too large

prox.InputHoldBegin:Connect(function()
 start = DateTime.now().UnixTimestampMillis*0.001
end)

prox.InputHoldEnd:Connect(function()
 local finish = DateTime.now().UnixTimestampMillis*0.001
 local difference = math.max(0, finish - start)
 local tweeninfo = TweenInfo.new(
  math.min(difference, prox.HoldDuration),
  Enum.EasingStyle.Bounce,
  Enum.EasingDirection.In
 )
 -- play open/close tween here
end)

But about the door knowing which direction to go based off of its current orientation, I don’t know how to code that (atleast with a proximity prompt) but i have a suggestion that might help you brainstorm a solution for it.


Variable that determines if the door is open outwards, Closed, Or open inwards with the help of two attachments and math.sign(), The name of the variable for example will be “open”

The two attachments will be connected to the door, One of them 90 degrees clockwise, And the other 90 degrees counter-clockwise, I recommend changing the orientation of the attachments to face whichever orientation youd like the door to rotate to too.

As mentioned before, I don’t know how to check which side of the door the player is facing, So for the sake for this example i will assume its a boolean variable called “isfacingfront”

local isfacingfront
local open = 0
local tweenservice = game:GetService("TweenService")
local prox = script.Parent
local inwards : Attachment = prox.Parent.Inwards
local outwards : Attachment
-- assuming the proximity prompt is under the door that contains the 2 attachments i mentioned previously.
local tween = {
 IN = tweenservice:Create(
  prox.Parent,
  tweeninfo, -- i know i havent set this variable but just pretend it's set to your preferred arguments
  {CFrame = inwards.WorldCFrame}
 ),
  OUT = tweenservice:Create(
  prox.Parent,
  tweeninfo,
  {CFrame = outwards.WorldCFrame}
 )
}

prox.InputHoldEnd:Connect(function()
 local attachment = if isfacingfront then inwards else outwards
 if math.abs(math.sign(attachment.Orientation.Y)+open) < 2 then
  tween[if isfacingfront then "IN" else "OUT"]:Play()
  -- any code you want to add
  open += math.sign(attachment.Orientation.Y)
 else -- door goes the other way (towards the player)
  tween[if isfacingfront then "OUT" else "IN"]:Play()
  -- any other code you want to add
  open -= math.sign(attachment.Orientation.Y)
 end
end)

The two scripts aren’t supposed to be connected or anything by the way, But sorry for any bad typing/formatting i haven’t noticed.

Let me know if you need any explanation on this.

I think 2 posts above he mentioned that this isn’t what he was looking for :frowning:

Edit: here’s his updated requirement

So the door is supposed to open without any interaction other than going up to it, Like in TF2, right? Maybe putting two invisible parts on the front and back of the door would help.

I’m not sure this is quite what I’m trying accomplish.

I’m try to have it where when the Input Begins, the door opens depending on the players direction of movement. (Not sure if any of y’all have played Outlast but in that game, their door mechanics are what I’m trying to “recreate” of sorts)

  • Outlast door mechanics that I want to use are the ability to slowly or instantly open/close doors using the players walk direction (so when they move in a forward/backward motion).