Hey there, I think I'm really going to love your website, it seems like there's a lot of community help here, so I'm asking for help on here, and not the roblox developer forums because of stupid rules and practically no help.
I'm trying to achieve:
A door that opens when clicked, and closes on the second click, without looking like someone just teleported the door to 2 different points.
My entire code is as follows: (it's not too long)
local Door = game.Workspace:WaitForChild("Door") local Hinge = game.Workspace:WaitForChild("Hinge") local HingePos = Hinge.Position local Duration = 2 local OpenOrClosed = true Runfunction = true deg = 90 local function OnClick() if OpenOrClosed == true then --It's closed right now. open it right now: Do this. deg = 90*(tick() % Duration)/Duration while deg < 91 do deg = 90*(tick() % Duration)/Duration print(deg) Door.CFrame = CFrame.new(HingePos) * CFrame.Angles(0, math.rad(deg), 0) * CFrame.new(2.2, 0, 0) wait() if deg > 85 then deg = 90 and OpenOrClosed == false end end else if OpenOrClosed == false then --It's open right now, close it right now: Do this. while deg >0 do deg = -90*(tick() % Duration)/Duration print(deg) Door.CFrame = CFrame.new(HingePos) * CFrame.Angles(0, math.rad(deg), 0) * CFrame.new(2.2, 0, 0) wait() if deg <=5 then deg = 0 and OpenOrClosed == true Runfunction = false end end end end end game.Workspace.Door.ClickDetector.MouseClick:connect(OnClick)
The logic:
I have a couple of variables, door and hinge
hinge is equal to my workspace variant "Hinge" duration is just how long it takes (in seconds, ish)
deg is equal to 90 (number of degrees) and the amount of time it should take for those degrees to 'animate' (which is tick, duration/ duration)
I've printed deg to the log at this point
now where it actually happens:
door.cframe, the doors cframe is = to the hinge pos, times angle, math rad(which is deg), then I specify how far it should be away from the hinge while rotating.
then, to get out of the loop that would happen if I let the code run and run, I put in an if statement saying that if it's 89 with the variable isopen false, to set it to true. and if it's 179 when it's true, to set it to false.
then just a bunch of ending stuff and a click detector I plan to add in a GUI that shows up when you're looking at the door, later, once I get this actually working.
The problem:
in my output I get strange readings, and it (my door) jumps all over the place. It jumps to 50* in the first frame. I'll paste a small bit of my output below:
Door was opened 50.011413806194
50.825720864373
51.621655515722
52.447062569696
53.247103175601
54.053476694468
54.850339245152
55.682554760495
56.489670598829
57.335607425587
58.107074531349
58.921555570654
59.751375945839
60.543454015577
61.3487024565
62.166141174935
62.977792121269
63.776301693272
64.5997485599
65.39623995085
66.201784159686
67.030821619807
67.847361435761
68.660450626064
69.452221329148
70.279391391857
71.081235602095
71.881914138794
72.712215861759
73.516165243613
74.340313834113
75.162989384419
75.941044575459
76.772013226071
77.569142547814
78.371850864307
79.204466536238
79.999171720969
80.810434109456
81.636165928196
82.416680052474
83.24547973839
84.048443227201
84.845305777885
85.676291826609
86.471304377994
87.283367079657
88.107811438071
88.902748597635
89.736094990292
0.53571688162314
it resets, and that's not what I want!
I want it to get to the nearest it can to 90 (it's not always consistent. sometimes it'll go to 89, sometimes it'll go to 87, etc, depending on what I have duration set to {lower means faster, so the faster I go the more sporadic it is.})
I decided, I'd try and get it to open a full 180 degrees and have that stable before trying to figure out why it doesn't want to go back to 0, 0, 0.
Summary: I want the door to go to 90, not 89, not 87, not 86, 90, then allow a second click to close it the same way it opened.
LEGENDARY SCRIPTERS HELP ME PL0X
Well there are two ways of doing this, welding the door parts together and then using 'for i = 1, #NumberHere do' and have it change the CFrame angles, or you can do the following:
Door:SetPrimaryPartCFrame(Door:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(math.pi/2),0))
Hope this helps!
local part = script.Parent local tween = game:GetService("TweenService") local en = {} en.Orientation = Vector3.new(0,90,-90) local tweeninfo = TweenInfo.new( 5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0 ) local open = tween:Create(part, tweeninfo, en) wait() open:Play()