How to program a door in roblox

Hey guys! Today , I will be teaching you how I made my door model
It is a drag and drop door which is easy to use regardless of what your door design is!

How it is made:
DISCLAMER THIS IS AN OVERSIMPLIFIED EXPLANATION ALL THE SCRIPT TYPED IN THE EXPLANATION WILL NOT WORK IF YOU JOIN THEM TOGETHERE

SCROLL ALL THE WAY DOWN TO STEAL THE FREE MODEL LOL

So to open the door , we will be using PropximityPrompt

local DoorOpen = false
ProximityPrompt.Triggered:Connect(function(plr)
--fires when proximity prompt activated by a player
end)

now lets say the door is close and we fire the proximity prompt . This action should cause the door to open . Thus , we will use this if statement

if DoorOpen == false then
DoorOpen = true
else
DoorOpen = false
end

your script now should look like this

local DoorOpen = false
ProximityPrompt.Triggered:Connect(function(plr)
--fires when proximity prompt activated by a player
     if DoorOpen == false then
          DoorOpen = true
          Open()
     else
          DoorOpen = false
          Close()
     end
end)

Now since the door model I made use a CFrame hinge system, we will be using tweening to rotate the hinge. You might be wondering how the door open when the hinge rotate … well … I welded the door to the hinge and made the door unanchored .

Problem: Well… we cannot really Tween an anchored object … can we???

Solution: We can create a CFrameValue and tween the CFrame value instead . We then write an event which fires when the CFrameValue’s CFrame change

script:

local TS = game:GetService("TweenService")

local CFrameValue = Instance.new("CFrameValue",hinge)

local OpenGoal = {}
OpenGold.Value = HingeCloseCF*CFrame.Angle(0,math.rad(openAngle),0)
local OpenTween = TS:Create(CFrameValue,TweenInfo.new(doorOpenTime),OpenGoal)

local CloseGoal = {}
CloseGoal .Value = HingeCloseCF
local CloseTween = TS:Create(CFrameValue,TweenInfo.new(doorOpenTime),OpenGoal)

That script basically Handle the CFraming of the door . So in theory , the script should look like this

local DoorOpen = false
ProximityPrompt.Triggered:Connect(function(plr)
--fires when proximity prompt activated by a player
     if DoorOpen == false then
          DoorOpen = true
          Open()
     else
          DoorOpen = false
          Close()
     end
end)

function Open()
     local OpenGoal = {}
     OpenGold.Value = HingeCloseCF*CFrame.Angle(0,math.rad(openAngle),0)
     local OpenTween = TS:Create(CFrameValue,TweenInfo.new(doorOpenTime),OpenGoal)
     OpenTween:Play()--Play the tween
end
function Close()
     local CloseGoal = {}
     CloseGoal .Value = HingeCloseCF
     local CloseTween = TS:Create(CFrameValue,TweenInfo.new(doorOpenTime),OpenGoal)
     CloseTween:Play()--play the tween
end

and now we have a simple working door . However, lets make it dynamic . Dynamic as in when the player open the door, the door will never come swinging into their face. To make this , we will need to use dot product(kinda hard to learn ngl xD). So in theory , when the player is 90 degree Infront of the door, the dot product should be 1 , when the player is 90 degree behind the door, the dot product should be -1 . So if the player is 180 degrees Infront the door , the dot product will be higher than 0 while if the player is 180 degrees behind the door , the dot product will be lower than 0

function GetDotProduct(doorCF,CharCF)
     local DoorPosToCharPos = (doorCF.Position - CharCF.Position).Unit
     local DoorLookVector = doorCF.LookVector
     local Dot = doorLookVector(DoorPosTOCharPos)
     return Dot > 0 and 1 or -1 -- basicall check if dot is bigger than 0 . if it is , it will return 1 . Else, it will return -1
end

splendid! So to change the direction of the rotation of the door when open, we can use this scirpt

local DoorOpenAngle = DoorOpenAngle * GetDotProduct(Door.CFrame,Char.HumanoidRootPart.CFrame)

Now , here is the free model
you can open settings to change some settings base on your own liking
image
Do not delete anything that is called Base ,Root ,Hinge ,Frame ,Door and the scripts
you can delete any descendance of Frame and Door
you can anchore/unachore the model as the script will handle the anchoring of the model

you can steal the free model without giving me credit but please understand the script <3

open for any suggestions to improve the door :>

8 Likes

Remember the slvcs situation? He made a topic called “How to build a door” and he put pictures with blood and gore


Im going to stay on topic, anyways what about the building part? you only provided the scripting part :confused:

3 Likes

you are suppose to design your own door ;-;
sorry if the word “build” is misleading

but to make the door model work with the script , you just need to empty out the descendance of the model in the door model called “Door” and “Frame” and put your own door and frame model . you dont have to ungroup the model for it to weld together

3 Likes

This might be off-topic, but you can replace this code snippet with:

DoorOpen = not DoorOpen

It does the same thing except in only 1 line of code.

4 Likes

Hmm I dont think it is possible cuz from what I remember , roblox do not really like = and “not” together because there is ~= . However, It is late at night for me and I do not have any chance to test it so I will do a follow up tmr.

yes it is possible to do x = not y and even x = not not not not y (which just simplifies to y)
image

1 Like

hmm cool . I tried his code but it did not work if this work it will be great for things that toggle cuz it is only a line lol . Ty for your time :slight_smile:

1 Like

oh yea ty for telling me dis . it is life changing lol

image

not even purposefully nsfw/racist posters get this type of (deserved) treatment. they usually just get booted off the devforum, not the entire platform.