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)
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)