Opening door both ways depending on where player touched

You can write your topic however you want, but you need to answer these questions:
Im currently working on a door. The door is going to open when player touches it, simple.

My issue is that the door only opens one way. So it works fine when a player opens it from one way. But if the player goes into the door the other way, the door opens in the wrong direction. I want the door to open outwards every time.

I dont know if I have to use lookvector but im not sure. I tried searching but didn’t really find anything. Maybe there is something out there but not what I found.

I have a local script and a module script for tweening.

local scrip

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer

local doorModel = game.Workspace:WaitForChild("Door2")
local doorPart = doorModel:WaitForChild("doorPart")

local TweenModule = require(ReplicatedStorage:WaitForChild("ModuleScript"))

local debounce = true
local isOpen = false


function onCharLoad(char)
	doorPart.Touched:Connect(function()
		if debounce and isOpen == false then
			
			--opening door
			TweenModule:MoveDoor(
				doorPart,
				.5,
				Vector3.new(40.4, 5.7, -10.85),
				Vector3.new(5.85, 11.2, 0.5),
				Vector3.new(0, 0, 0)
			)
			isOpen = true
			debounce = false
			task.wait(2)
			debounce = true
			
			--closing door
			TweenModule:MoveDoor(
				doorPart,
				.5,
				Vector3.new(37.45, 5.7, -7),
				Vector3.new(6.9, 11.2, 0.8),
				Vector3.new(0, -90, 0)
			)
			
			isOpen = false
		end
	end)
end

player.CharacterAdded:Connect(onCharLoad)

if player.Character then
	onCharLoad(player.Character)
end

module:

local TweenModule = {}

local TweenService = game:GetService("TweenService")

function TweenModule:MoveDoor(part, tweenTime, endPos, endSize, endRot)
	local tweenInfo = TweenInfo.new(
		tweenTime,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.InOut
	)
	local goal = {
		Position = endPos,
		Size = endSize,
		Orientation = endRot
	}
	
	TweenService:Create(part, tweenInfo, goal):Play()
end

return TweenModule

thank you!

1 Like

You could calculate which way the player is looking, or which side of the door the player is at, indeed, but an easier and more efficient way would be simply setting up two different triggers, one for each side of the door, and detecting them separately, and in your script, making it open the opposite way depending on which one of the two triggers was triggered.

Those triggers can be whatever you wish them to be; A part to detect “.Touched”, a ProximityPrompt, Etc.

3 Likes

man I didnt think of that. thank you

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.