How can I tween both the orientation and position using CFrame

How do I make this script tween both orientation and position (using :lerp() is not an option due to it causing lag)

local TweenService = game:GetService("TweenService")
local Speed = 10 -- How many studs you want to go per second

local function calcDist(point1, point2)
	return (point1 - point2).magnitude
end

local wp = 1

while game.Workspace.Waypoints:FindFirstChild(tostring(wp)) do
	local Pos1 = script.Parent.CFrame
	local Pos2 = CFrame.new(game.Workspace.Waypoints[tostring(wp)].Position)

	local Distance = calcDist(Pos1.Position, Pos2.Position)
	print(Distance)
	local Time = Distance/Speed
	print(Time)

	local TweenInf = TweenInfo.new(Time, Enum.EasingStyle.Linear)
	local TweenGoal = {CFrame = Pos2}
	

	TweenService:Create(script.Parent, TweenInf, TweenGoal):Play()

	wait(Time) -- Wait for the tween to finish before moving to the next waypoint
	wp = wp + 1
end

could you try this

local TweenGoal = {
        CFrame = Pos2,
        Orientation = Pos2 - Pos1 

    TweenService:Create(script, Tween.ParentInf, TweenGoal):Play()

Unfortunately, I got the error

Workspace.Train.Script:22: invalid argument #2 (Vector3 expected, got CFrame)

mb try this

   local TweenGoal = {
        CFrame = Pos2       ,
 Orientation = Pos2 - Pos1.Position -- Calculate the orientation based on the difference between Pos2 and Pos1
    }

    TweenService:Create(script.Parent, TweenInf, TweenGoal):Play()

TweenService:Create property named 'Orientation' cannot be tweened due to type mismatch (property is a 'Vector3', but given type is 'CoordinateFrame')

sorry but could you provide a video or ss of this script working

Yep heres a video of the original script
https://gyazo.com/508726a16e4566bf31bdbae3cbefa3a5

try this

local TweenService = game:GetService("TweenService")
local Speed = 10 -- How many studs you want to go per second

local function calcDist(point1, point2)
    return (point1 - point2).magnitude
end

local function createTween(obj, property, goal, time, style)
    local tweenInfo = TweenInfo.new(time, style)
    local tweenGoal = {}
    tweenGoal[property] = goal

    return TweenService:Create(obj, tweenInfo, tweenGoal)
end

local wp = 1

while game.Workspace.Waypoints:FindFirstChild(tostring(wp)) do
    local startPos = script.Parent.CFrame
    local endPos = CFrame.new(game.Workspace.Waypoints[tostring(wp)].Position)

    local distance = calcDist(startPos.Position, endPos.Position)
    print(distance)

    local moveTime = distance / Speed
    print(moveTime)

    local rotationTween = createTween(script.Parent, "Rotation", endPos - startPos, moveTime, Enum.EasingStyle.Linear)
    local positionTween = createTween(script.Parent, "Position", endPos.Position, moveTime, Enum.EasingStyle.Linear)

    rotationTween:Play()
    positionTween:Play()

    wait(moveTime) -- Wait for the tweens to finish before moving to the next waypoint
    wp = wp + 1
end

I could be worng but if you’ve got a CFrame.new() value doesnt that use the position and not orientation?

1 Like

welp I give up and ur right CFrame.new() value uses the position and not orientation?
I’ll try to help you but at the same time GoodLuck On Finding an aNSWer

Pos1 - Pos2.Position will give you another cframe because Pos1 is a cframe and cframe - vector3 is a cframe. You should use parantheses. Idk why you are taking difference of positions to rotate an object tho

May I ask how you would go about solving this issue?

local part = game.Workspace.Waypoints:FindFirstChild(tostring(wp))
game.TweenService:Create(script.Parent, TweenInf, {Position = part.Position, Orientation = part.Rotation}):Play()

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