How would you tween a part to a moving part?

Basically title. I don’t really know if there is a way to do this, but if there is please respond
The issue with this is it’ll stop after it reaches its goal position and it won’t just keep going to the part. (If there is a way to do this worth out tweening that is also okay)

Write a loop and code to tween the part manually, always targeting the latest position on of the targetPart.

There may be a hacky solution about welding the tweening part to the moving part, and tweening the weld C0.

1 Like

Could you give me a short example of that?

First off, you need to create a tween (obviously)

local myTweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Back,
	Enum.EasingDirection.In,
	0,
	false,
	0
)

Then apply this to a part by doing

local myTweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.In,
	0,
	false,
	0
)

local TweenService = game:GetService("TweenService")
local partToTween = game.Workspace.Part1
local partToFollow = game.Workspace.Part2

local Tween = TweenService:Create(partToTween, myTweenInfo, {CFrame = CFrame.new(partToFollow.CFrame)}

Then finally, to constantly follow said partToFollow, you need to loop it. You can do this multiple ways, and it depends on what you want to do it for. If you want it to ALWAYS follow the part, then While true do is the way to go, which can be accomplished by doing

local myTweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.In,
	0,
	false,
	0
)

local TweenService = game:GetService("TweenService")
local partToTween = game.Workspace.Part1
local partToFollow = game.Workspace.Part2

while true do
     local Tween = TweenService:Create(partToTween, myTweenInfo, {CFrame = 
     CFrame.new(partToFollow.CFrame)}
     wait(1) -- You can get rid of this depending on what you want
end

You may want to change it to only follow the part until a value is changed or something is clicked etc. If so, then you do the research. Any questions you can just ask :grinning:

Ok thanks! A question I have is

Is there any way to make a wait() in an I,v pair that doesn’t slow down the speed?
Example


Local table = {one,two,three,four,five}

for I,v in pairs(table) do
wait(1)
print(v)
end

Any way to make it print all 5 in one second? (Without putting any wait() function outside of the loop)

One way would be to simply change the wait time to be wait(1/SizeOfTable)

Not saying this is the best or only way to do it. This is just one of many.

function tweenToMovingPart(movePart, targetPart, duration)
	local startCFrame = movePart.CFrame
	local startTime = os.clock()
	local connection = nil
	connection = game:GetService('RunService').Heartbeat:Connect(function()
		local t = math.clamp((os.clock()-startTime)/duration, 0 ,1)
		if movePart and movePart.Parent and targetPart and targetPart.Parent then
			movePart.CFrame = startCFrame:Lerp(targetPart.CFrame, t)
		end
		if t == 1 then
			connection:Disconnect()
			connection = nil
		end
	end)
end

-- Examples

wait(5)

tweenToMovingPart(workspace.MovePart, workspace.TargetPart, 5)

for i,v in pairs(workspace.Model:GetChildren()) do
	tweenToMovingPart(v, workspace.TargetPart, 5)
end

I will clean this code up in a minute, but this is the gist of it.

Edit: Code clean up done.

3 Likes

Ummm, if you want it all at once the do


Local table = {one,two,three,four,five}

for I,v in pairs(table) do
   print(v)
end

but if you want to print it over a course of one second at even intervals, you simply just need to do some math. 1/5 = 0.2, meaning that you can do


Local table = {one,two,three,four,five}

for I,v in pairs(table) do
   print(v)
   wait(0.2)
end

If that doesn’t answer your question, please rephrase what you are saying because I’m not sure what you mean then :smile: