Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Keeping an objects rotation when moving it with CFrame?

Asked by 4 years ago

Hello,

As from my last post, I have been able to make a script that allows the door that I'm working on to move up and down from the click of a button. The only issue I have now is that upon pressing the button, the door will rotate 90 degrees, making it look weird and out of place.

I've also had a lot of issues with this in the past, yet just scrap the idea as I can never figure out a way to fix this.

local LeftDoor = script.Parent.Parent.Parent.LeftOfficeDoor
local RightDoor = script.Parent.Parent.Parent.RightOfficeDoor
local Sound = script.Parent.Sound
local debounce = false
local door = false
local Rotation = LeftDoor.CFrame - LeftDoor.Position

script.Parent.ClickDetector.MouseClick:connect(function()
    if not debounce then debounce = true
    if not door then door = true
            Sound.Playing = false
            Sound.Playing = true
        for i = 1,14 do
            LeftDoor.CFrame = CFrame.new(LeftDoor.Position + Vector3.new(0,1,0))
            wait()
        end
               debounce = false
    else if door then door = false
            Sound.Playing = false
            Sound.Playing = true
            for i = 1,14 do
            LeftDoor.CFrame = CFrame.new(LeftDoor.Position - Vector3.new(0,1,0))
            wait()
end
      debounce = false
    end
  end
end
end)

The door works perfectly fine, its just the rotation. I have seen many forms about this, yet the explaining on them is very little and I have no idea where to place them into my script.

If anyone could help, it would be greatly appreciated.

2 answers

Log in to vote
1
Answered by 4 years ago

Try this (may not be the best solution)

local RunService = game:GetService("RunService")
local LeftDoor = script.Parent.Parent.Parent.LeftOfficeDoor
local RightDoor = script.Parent.Parent.Parent.RightOfficeDoor
local Sound = script.Parent.Sound
local debounce = false
local door = false
local Rotation = LeftDoor.CFrame - LeftDoor.Position

RunService.RenderStepped:Connect(function()
    LeftDoor.Orientation = Vector3.new(0,0,0)-- Put the original Orientation of the door in the brackets
    RightDoor.Orientation = Vector3.new(0,0,0)-- Put the original Orientation of the door in the brackets
end)

script.Parent.ClickDetector.MouseClick:connect(function()
    if not debounce then debounce = true
    if not door then door = true
            Sound.Playing = false
            Sound.Playing = true
        for i = 1,14 do
            LeftDoor.CFrame = CFrame.new(LeftDoor.Position + Vector3.new(0,1,0))
            wait()
        end
               debounce = false
    else if door then door = false
            Sound.Playing = false
            Sound.Playing = true
            for i = 1,14 do
            LeftDoor.CFrame = CFrame.new(LeftDoor.Position - Vector3.new(0,1,0))
            wait()
end
      debounce = false
    end
  end
end
end)
0
I had just done this before you posted it and it works, so thanks anyway! DiamondRules01 56 — 4y
0
aw thanks for excepting my answer anyways :D Have a good day! NarwhalAndMe 141 — 4y
0
oh darnit i did the thing wrong though :/ NarwhalAndMe 141 — 4y
0
oh darnit i did the thing wrong though :/ NarwhalAndMe 141 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Aha, didn't really expect it to be this easy. I fixed it by changing the orientation in the script each time.

local LeftDoor = script.Parent.Parent.Parent.LeftOfficeDoor
local RightDoor = script.Parent.Parent.Parent.RightOfficeDoor
local Sound = script.Parent.Sound
local debounce = false
local door = false
local Rotation = LeftDoor.CFrame - LeftDoor.Position

script.Parent.ClickDetector.MouseClick:connect(function()
    if not debounce then debounce = true
    if not door then door = true
            Sound.Playing = false
            Sound.Playing = true
        for i = 1,14 do
            LeftDoor.Orientation = Vector3.new(0, -90, 0)
            LeftDoor.CFrame = CFrame.new(LeftDoor.Position + Vector3.new(0,1,0))
            LeftDoor.Orientation = Vector3.new(0, -90, 0)
            wait()
        end
               debounce = false
    else if door then door = false
            Sound.Playing = false
            Sound.Playing = true
            for i = 1,14 do
            LeftDoor.Orientation = Vector3.new(0, -90, 0)
            LeftDoor.CFrame = CFrame.new(LeftDoor.Position - Vector3.new(0,1,0))
            LeftDoor.Orientation = Vector3.new(0, -90, 0) 
            wait()
end
            debounce = false
    end
  end
end
end)

Answer this question