Code works incorrectly

I’ve got a code and it does not work as intended.

I have a blast door, and 2 door models inside it, and I wanna move them so that the door is opened. The position is moving correct, but also it rotates the door 90 degrees when opening. And it does not rotate it -90 degrees when closing.

I know the issue, it’s because CFrameValue that gets created has orientation of 0,0,0. How to set it to 0,90,0?

This issue is causing me to go insane, please help.

The clear code:

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(12.91)
local Door1 = script.Parent.Door1
local Door2 = script.Parent.Door2
local TweenedCompleted = "N/A"
local DoorStatus = "Closed"
local DoorLocked = false
local Remote = Instance.new("BindableEvent",script.Parent)
local Debounce = false
local De

local function TweenModel(Model, CFrame, Wait)
    local CFrameValue = Instance.new("CFrameValue")
    CFrameValue.Value = Model:GetPrimaryPartCFrame()

    CFrameValue:GetPropertyChangedSignal("Value"):connect(function()
        Model:SetPrimaryPartCFrame(CFrameValue.Value)
	end)

    local Tween = TweenService:Create(CFrameValue, Info, {Value = CFrame})
    Tween:Play()

    Tween.Completed:connect(function()
        CFrameValue:Destroy()
		TweenedCompleted = "Yes"
    end)
	while TweenedCompleted == "N/A" and Wait == true do
		wait(0.1)
	end
	TweenedCompleted = "N/A"
end

local function Open()
	TweenModel(script.Parent.Door1, CFrame.new(script.Parent.Door1.Center.Position.X,script.Parent.Door1.Center.Position.Y,script.Parent.Door1.Center.Position.Z + 8),false)
	TweenModel(script.Parent.Door2,CFrame.new(script.Parent.Door2.Center.Position.X,script.Parent.Door2.Center.Position.Y,script.Parent.Door2.Center.Position.Z - 8),true)
	DoorStatus = "Opened"
end

local function Close()
	TweenModel(script.Parent.Door1, CFrame.new(script.Parent.Door1.Center.Position.X,script.Parent.Door1.Center.Position.Y,script.Parent.Door1.Center.Position.Z - 8),false)
	TweenModel(script.Parent.Door2,CFrame.new(script.Parent.Door2.Center.Position.X,script.Parent.Door2.Center.Position.Y,script.Parent.Door2.Center.Position.Z + 8),true)
	DoorStatus = "Closed"
end

--[[Removed the triggers for easier read on forum]]--
3 Likes

It seems like the rotation issue you’re facing is due to the orientation of the CFrameValue not being set correctly. To fix this, you can adjust the orientation of the CFrameValue to match the desired rotation of the door.

Here’s how you can modify your TweenModel function to set the orientation of the CFrameValue to 0, 90, 0:

local function TweenModel(Model, CFrame, Wait)
    local CFrameValue = Instance.new("CFrameValue")
    CFrameValue.Value = Model:GetPrimaryPartCFrame()
    -- Set the orientation of the CFrameValue
    CFrameValue.Value = CFrameValue.Value * CFrame.Angles(0, math.rad(90), 0)

    CFrameValue:GetPropertyChangedSignal("Value"):connect(function()
        Model:SetPrimaryPartCFrame(CFrameValue.Value)
    end)

    local Tween = TweenService:Create(CFrameValue, Info, {Value = CFrame})
    Tween:Play()

    Tween.Completed:connect(function()
        CFrameValue:Destroy()
        TweenedCompleted = "Yes"
    end)
    while TweenedCompleted == "N/A" and Wait == true do
        wait(0.1)
    end
    TweenedCompleted = "N/A"
end

This modification should ensure that the doors rotate correctly when opening and closing. If you encounter any further issues or have any questions, feel free to ask!

Doesn’t work, and I could ask ChatGPT for help myself.

That wasn’t ChatGPT, that was someone in my Discord DM’s answering :joy:

We had this problem a while back and I sent exactly what they sent me.

Also did it kick back any errors when run?

Yeah, it said Angles is not a valid member of CFrame - Server - Tween Script:15

Replace:

CFrameValue.Value = CFrameValue.Value * CFrame.Angles(0, math.rad(90), 0)

With:

CFrameValue.Value = CFrameValue.Value * CFrame.Angles(math.rad(0), math.rad(90), math.rad(0))

This might fix that.

It gives the same error in the output.

It isn’t as efficient but try:

CFrameValue.Value = CFrameValue.Value * CFrame.fromOrientation(0, math.rad(90), 0)

Error:
fromOrientation is not a valid member of CFrame

I’m not sure. Either it’s a syntax problem that I’m missing or the line where you’re creating the CFrame through Instanxe.new() is messing up.

What @MochaTheDev gave you should work. The reason an error occurs is because your function has a parameter with the same name as the datatype (CFrame). Whenever you put:

CFrame.Angles

It refers to the CFrame parameter, not the data type. Just change the name.

One of the variables passed in the function is literally named “CFrame” probably why you can use CFrame.Angles

Let me try real quick…

It kinda worked, I don’t really understand why it does this:
robloxapp-20240319-1831199.wmv (2.0 MB)

Fixed it. The solution was to change the:

local Tween = TweenService:Create(CFrameValue, Info, {Value = CFrame})

to:

local Tween = TweenService:Create(CFrameValue, Info, {Value = CFrame2 * CFrame.Angles(0, math.rad(90), 0)})

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.