Hello im trying to make a simple opening-closing door script with a model but i cant find any helpfull tutorial that worked for me
here’s what it should look like
thanks for responding
Hello im trying to make a simple opening-closing door script with a model but i cant find any helpfull tutorial that worked for me
here’s what it should look like
thanks for responding
Do you want it to be smooth using something like tweening? or do you just want it to be instant?
i would like to have it tweened
So, after I am done with school, I will look through a script I made that tweens a door to open and close and find the part that tweens it and give it to you. The method I use for mine is having an invisible point that the door part rotates on, kind of like a hinge.
Alright thanks i have time so i’ll be waiting
Make a hinge, put it on the door’s left and size it however you want.
After that, weld the hinge to the parts.
Use TweenService and tween the orientation of the hinge.
Thats what i tried to do multiple times but i always resulted to fails
Try to set the pivot of the door to the left side (you can do this through Edit Pivot tool in the Model tab)
Then you can just tween the whole door model’s orientation
It should be working. That’s the method i’m using
I cant understand about tweening the whole door model’s orientation if you could give an example or a screenshot it would maybe help me
Sorry for not being specific, I mean just tween the door model normally
Hey, instead of using a hinge, you can just combine an invisible door model that has cancollide off to the back of the first one so that it makes it so that when you tween its orientation, it seems like a hinge is their even though there isn’t. It just makes the center of mass where it would normally rotate change
local door = script.Parent -- Assuming the script is inside the door model
local isOpen = false -- Initialize the door as closed
local function openDoor()
if not isOpen then
door.CFrame = door.CFrame * CFrame.new(0, 5, 0) -- Adjust the second value for how high you want the door to open
isOpen = true
end
end
local function closeDoor()
if isOpen then
door.CFrame = door.CFrame * CFrame.new(0, -5, 0) -- Adjust the second value to close the door
isOpen = false
end
end
-- You can use a trigger or button to call these functions to open and close the door
-- Example: When a player touches the door, open it
door.Touched:Connect(function(other)
if other.Parent and other.Parent:FindFirstChild("Humanoid") then
openDoor()
end
end)
-- You can also create another script or event to close the door after some time or with a button press.
this will be a script in the model and when your character touches the door it should open.
Hey, sorry it took a while. I messaged in 2nd period so now I have it.
local TweenService = game:GetService("TweenService")
local click = script.Parent.ClickDetector
local open = false
local door = script.Parent
local Info = TweenInfo.new (
.8,
Enum.EasingStyle.Sine
)
local pivotHingeInitialCFrame = door.HingeAttachment.CFrame
local offset = pivotHingeInitialCFrame:Inverse()*door.CFrame
local hingeCFrameValue = Instance.new("CFrameValue")
hingeCFrameValue.Value = pivotHingeInitialCFrame
hingeCFrameValue.Changed:Connect(function(newHingeCFrame)
door.CFrame = newHingeCFrame*offset
end)
local function Close()
local CloseGoal = {}
CloseGoal.Value = pivotHingeInitialCFrame
local Tween = TweenService:Create(hingeCFrameValue,Info,CloseGoal)
Tween:Play()
Tween.Completed:Wait()
open = false
end
local rotationAmount = math.rad(90) -- you can change this if you want
local function Open()
local OpenGoal = {}
OpenGoal.Value = pivotHingeInitialCFrame*CFrame.Angles(0,rotationAmount,0)
local Tween = TweenService:Create(hingeCFrameValue,Info,OpenGoal)
Tween:Play()
Tween.Completed:Wait ()
open = true
end
Something to know with this is that you are going to need to create a hinge part(It can be just a basepart) and then position it exactly where you want it to be where the actual doors hinges would be. Also name the hinge HingeAttachment.
Place this script as a server script underneath the part of the model you are going to tween. If it is more than one part, get the main part and weld everything else to that part.
Let me know if this works out for you. You can simply use a clickdetector or a proximity prompt to open and close it after adding the variables for them and an event that happens when it is interacted with.
edit: Also just check if open is true or false and then do the Close or Open functions based on that
The best way to get a door that moves smoothly everywhere and works with the physics engine is to simply use a HingeContraint in Servo actuator mode. Your script then only has to set the TargetAngle to open or close the door. I’ve attached an example place file that shows how to set up a basic door, and has a dead-simple script that does nothing more than alterately sets the target angle to open and close it.
BasicDoorSetup.rbxl (53.9 KB)
Everything about how the door animates is configured with the hinge servo properties, including how fast it moves, range of motion, and how much torque is applied. Low torque will let players hold the door open by blocking it, very high torque will make the door shove the players. I put NoCollisionContraints between the door panel and frame parts, so that the door won’t jam if the parts overlap or rub a bit when animating.
Don’t try to manually CFrame the door parts and don’t use TweenService for a physical door, these are noob mistakes that result in doors that don’t move smoothly for everyone, or aren’t in sync with physics simulation everywhere. AlignOrientation is OK to use, but more complicated to set up and not necessary for a simple rotating door.
Thanks for this very detailled explanation i can understand now what was my mistakes
if my main part is an union is it still working ?
Yes, if your main part is a Union then you will be able to do it. Unions function kind of like normal baseparts, so you will be able to tween it.
Have you managed to get it to work?