I got this script that this site helped me make, but every time I make a new door, I have to tinkle and tweak the CFrame positions so it will open and close (and be in the correct place.) It becomes a hassle, and I dread the next time a level requires me to make a door
So, I want to make this door universal and be able to use in any position or place, but me being a noobie, I don't know where to start!
local Door = script.Parent local distanceToOpen = 10 local used = false function onClicked(playerWhoClicked) local dist = playerWhoClicked:DistanceFromCharacter(Door.Position) if dist <= distanceToOpen and not used then used = true Door.CFrame = CFrame.new(34.6, 12.3, 105.1)*CFrame.Angles(0,math.rad(-90),0)--opened print("door opened") wait(3) used = false Door.CFrame = CFrame.new(32.1, 12.3, 103.4)*CFrame.Angles(0,math.rad(0),0)--closed print("door closed") end end Door.ClickDetector.MouseClick:connect(onClicked)
(Script is inside an object (the door) and relies on a ClickDetector)
Think: What is changing whenever I make another door? The Door Position... Does that have a relation to the CFrames that the door moves to when closing and opening? Yes, those CFrames can relate directly to the door
http://wiki.roblox.com/index.php?title=CFrame#Methods
Use ToObjectSpace(DoorCFrame) which will return the difference in CFrame of a position compared the the Door's CFrame.
I'll give some code example:
doorPos = game.Workspace.Door.CFrame --you can reset this to the exact hierarchical position of the door in your place --next we can get the difference of the open and close CFrames relative to the door using ToObjectSpace currentOpenPos = CFrame.new(0, 5, 0) -- The current open position you are using in the above script openPos = currentOpenPos:toObjectSpace(doorPos) --We can get the difference --With this information, simply add the difference to find the new open door position whenever you change the position of a door newDoorPos = CFrame.new(0,5,0) --Change this to what you want newDoorOpen = newDoorPos + Vector3.new(openPos.X, openPos.Y, openPos.Z) --Then to change the rotation, simply multiply by CFrame.Angles(keep in mind we are using radians --This final line is giving me errors, I'm trying to get the rotation of openPos and in a sense add it to this CFrame. In the meantime you will have to make up your own rotations each time, should remain constant for this projec though newDoorOpen = newDoorOpen.CFrame.p * CFrame.Angles(0, math.pi/2, 0) --CFrame.p returns the 3D position of the object in vector3 form