What do you want to achieve?
I have a hinge based door system for a car and I want the door to open and close when entering or exiting the DriveSeat
What is the issue?
Can’t find a way to make the door move to the desired location
What solutions have you tried so far?
Tried using ai but no matter how hard I try I couldnt get them to work as I wanted and couldn’t find any tutorials related to bringing hingeconstraints based objects to desired locations
Please be as descriptive as you can because I lack of major scripting experience but I’ll try my best
-- Script.lua by VanillaEdge
-- For Left Door
STRUTS_ENABLED = false
MAX_ANGLE = 80
PLAYER_POWER = 80
STRUT_POWER = 10
-- Variables
local model = script.Parent
local doors = model.Parent
local car = doors.Parent.Parent
local driveSeat = car.DriveSeat
local openSound = doors.OpenSound
local closeSound = doors.CloseSound
local hingeConstraint = model.Hinge.HingeConstraint
local dragDetector = model.MainPart.DragDetector
local bodyForce = model.MainPart.BodyForce
local weld = model.MainPart:WaitForChild("Weld")
local lastDragFrame = dragDetector.DragFrame
local doorMovedByPlayer = false
local doorIsOpen = false
local bufferAngle = 0.3
-- Functions
local function updateLockState(state)
weld.Enabled = state
hingeConstraint.LowerAngle = state and 0 or MAX_ANGLE
end
local function updateDoorState()
local currentAngle = hingeConstraint.CurrentAngle
-- Play sound based on door state
if currentAngle >= bufferAngle and not doorIsOpen then
openSound:Play()
doorIsOpen = true
elseif currentAngle < bufferAngle and doorIsOpen then
closeSound:Play()
doorIsOpen = false
end
-- Apply strut force
if STRUTS_ENABLED and currentAngle >= bufferAngle then
local lookVector = driveSeat.CFrame.lookVector
bodyForce.Force = lookVector * STRUT_POWER
else
bodyForce.Force = Vector3.new(0, 0, 0)
end
-- Automatically lock/unlock door
if not doorMovedByPlayer then
local state = currentAngle < bufferAngle
updateLockState(state)
end
end
local function onDragDetectorChanged()
if lastDragFrame ~= dragDetector.dragFrame then
doorMovedByPlayer = true
lastDragFrame = dragDetector.dragFrame
updateLockState(false)
end
end
-- Connections
dragDetector:GetPropertyChangedSignal("DragFrame"):Connect(onDragDetectorChanged)
-- Initialise
dragDetector.Enabled = true
dragDetector.MaxForce = PLAYER_POWER
dragDetector.MaxTorque = PLAYER_POWER
hingeConstraint.LowerAngle = 0
weld.Enabled = false
while true do
task.wait(0.1)
updateDoorState()
doorMovedByPlayer = false
end
The door is under Misc-Doors-FL
Doors.rbxm (262.1 KB)