How do I tween something that is being tweened

I know that the quality sucks, it’s because I don’t want to upload these on some other website

  1. What do you want to achieve? Keep it simple and clear!
    I need to be able to tween everything
    Like this:


    And no, it’s not calculating the distance:

  2. What is the issue? Include screenshots / videos if possible!
    Mine isn’t working only does this and doesn’t move the model:

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked everywhere and asked for help, but it didn’t work. Also you can’t weld it because it needs to tween

My code:

local TweenService = game:GetService("TweenService")

local Type1 = script.Parent.Parent.Type1
local Type2 = script.Parent.Parent.Type2
local Type4 = script.Parent.Parent.Type4
local part = script.Parent
local onForward = true
local onBackward = false


local distanceFromType1 = part.Position - Type1.Position


local speed = 1

-- Finding Distances for Types
local function getDistanceToTarget(targetPosition)
	local currentX, currentY, currentZ = part.Position.X, part.Position.Y, part.Position.Z
	local targetX, targetY, targetZ = targetPosition.X, targetPosition.Y, targetPosition.Z

	return math.sqrt(
		(currentX - targetX) ^ 2 + (currentY - targetY) ^ 2 + (currentZ - targetZ) ^ 2
	)
end

-- Helper function to find distance (cleaner code)

-- Setting pos of part
while true do
	if _G[Type4.BrickColor.Name] == true and onForward == true then
		local distanceToType2 = getDistanceToTarget(Type2.Position)

		local goal = {Position = Vector3.new(part.Position.X + Type2.Position.X - part.Position.X + distanceFromType1.X, part.Position.Y + Type2.Position.Y - part.Position.Y + distanceFromType1.Y, part.Position.Z + Type2.Position.Z - part.Position.Z + distanceFromType1.Z)}

		-- Adjust time based on distance (optional)
		local tweenInfo = TweenInfo.new(speed, -- Adjust time based on distance
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.InOut,
			0, -- RepeatCount (0 = no repeat)
			false, -- Reverses (tween does not reverse)
			0 -- DelayTime
		)
		local tween = TweenService:Create(part, tweenInfo, goal)
		tween:Play()
		onBackward = true
		onForward = false
	elseif _G[Type4.BrickColor.Name] == false and onBackward == true then
		local distanceToType1 = getDistanceToTarget(Type1.Position)

		local goal = {Position = Vector3.new(part.Position.X + Type1.Position.X - part.Position.X + distanceFromType1.X, part.Position.Y + Type1.Position.Y - part.Position.Y + distanceFromType1.Y, part.Position.Z + Type1.Position.Z - part.Position.Z + distanceFromType1.Z)}

		-- Adjust time based on distance (optional)
		local tweenInfo = TweenInfo.new(speed, -- Adjust time based on distance
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.InOut,
			0, -- RepeatCount (0 = no repeat)
			false, -- Reverses (tween does not reverse)
			0 -- DelayTime
		)
		local tween = TweenService:Create(part, tweenInfo, goal)
		tween:Play()
		onForward = true
		onBackward = false
	end
	wait(.1)
end

Thanks!

3 Likes

you can stop a previous tween to tween to the tween that is new and stopping the old tween

2 Likes

could you explain more? I don’t really understand

2 Likes
local prevtween = nil

if prevtween
prevtween:Destroy()
end

local twen = tween
prevtween = twen

here’s jst an example
the previous tween will be destroyed when new tween is called

wait, I’m confused, I need to play two tweens
Here:

1 Like

do you want to tween two properties at once?

no, like in the video the model gets tweened and a part inside it

weld the parts in the model to the part you are tweening

no, then the welded part doesn’t tween

ok then use 999 level math to find out everything of answer of life

watch the videos, it shows tow parts tweening. One part gets tweened in a model and then that model gets tweened

yeah you have to use some math or make tween goals or something to do that

I need help with that :(. Please help

you can add parts that are the goals for the parts getting tweened going to

isn’t that the same as tweening to a position

yes that’s how tweening works, you define a goal to tween to and if you’re tweening a part thten you’re tweening its position

yes, I just don’t know how to do it like in the video
This one (below this text)

I think I have a fix @PreciseGaps, but I don’t know how to do it. I was thinking that I could make a position that’s only for the group (I don’t know if that possible)

So instead of moving from 3,5,8 to 3,8,10 it would be 0,0,0 (for the starting position/part) and then 0,3,2 (for the second position/part)

you have to loop through the parts and tween each

btw your getDistanceToTarget function is just an inferior version of (currentPosition - targetPosition).Magnitude

it’s inferior because .Magnitude is done in C++ which is more efficient