Door not opening/rotating properly

My door isn’t opening an how a door should open. I’m pretty bad at cframes and math so if anyone could help fix this that would be dope.

Video:

Script:

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

local openDoorEvent = ReplicatedStorage:WaitForChild("OpenDoorEvent")

local door = workspace:WaitForChild("Door")

local doorSound = workspace.Door:WaitForChild('DoorBust')

local doorOpened = false

local tweenInfo = TweenInfo.new(
	0.1, -- time
	Enum.EasingStyle.Linear, -- easing style
	Enum.EasingDirection.Out, -- easing direction
	0, -- times repeated
	false, -- reverses
	0 -- delay time
)

local hingePointRelative = Vector3.new(-door.Size.X / 2, 0, 0)
local targetCFrame = CFrame.new(door.Position) * CFrame.new(hingePointRelative):Inverse() * CFrame.Angles(0, math.rad(-55), 0) * CFrame.new(hingePointRelative)

local targetProperties = {
	CFrame = targetCFrame
}

openDoorEvent.OnServerEvent:Connect(function(player)
	if doorOpened then return end

	local character = player.Character

	if character then
		
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")

		if humanoidRootPart then
			
			local playerPosition = humanoidRootPart.Position
			local doorPosition = door.Position
			local distance = (playerPosition - doorPosition).magnitude

			if distance <= 5 then
				
				local tween = TweenService:Create(door, tweenInfo, targetProperties)

				tween:Play()
				doorSound:Play()
				doorOpened = true
			end
		end
	end
end)

This is were I did the cframe stuff:

local hingePointRelative = Vector3.new(-door.Size.X / 2, 0, 0)
local targetCFrame = CFrame.new(door.Position) * CFrame.new(hingePointRelative):Inverse() * CFrame.Angles(0, math.rad(-55), 0) * CFrame.new(hingePointRelative)

pls help

3 Likes

Try this adjustment in your code for the calculation of targetCFrame, let me know if it works for you.

local hingePointRelative = Vector3.new(-door.Size.X / 2, 0, 0)
local targetCFrame = door.CFrame * CFrame.new(hingePointRelative) * CFrame.Angles(0, math.rad(-55), 0) * CFrame.new(hingePointRelative)

here’s the adjusted evetn handler:
(prints added)

Additionally, you might want to check if the door is already opened before triggering the open animation. Currently, your code checks if doorOpened is true after the player interacts with the door. It might be better to check if the door is closed when the player interacts with it.

openDoorEvent.OnServerEvent:Connect(function(player)
	if doorOpened then 
		print("Door is already opened")
		return 
	end

	local character = player.Character

	if character then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")

		if humanoidRootPart then
			local playerPosition = humanoidRootPart.Position
			local doorPosition = door.Position
			local distance = (playerPosition - doorPosition).magnitude
			print("Distance from player to door:", distance)

			if distance <= 5 then
				print("Player is within interaction range")
				local tween = TweenService:Create(door, tweenInfo, targetProperties)
				tween:Play()
				doorSound:Play()
				doorOpened = true
			else
				print("Player is not within interaction range")
			end
		else
			print("HumanoidRootPart not found")
		end
	else
		print("Character not found")
	end
end)
2 Likes

I haven’t used this script but heres a slower version of whats happening.

Oh, I think that’s directly related to how your door is set up. Can I see the model and how the parts are arranged in it?

1 Like

the part is anchored.

image

I should prob use welds but idk how to apprpach this

nvm, welds don’t help cause of the script.

I recommend taking a look at this video for creating your door, it must fit correctly

Fixed it, and I made it so it opens the correct way no matter the direction your facing.

2 Likes

Brilliant! Congratulations, keep it up, I’m also learning step by step

1 Like

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