Moving a model in a tween

I’m trying to make a tween script that moves a model to a certain position, then resets to the original position and does it again.

local model = script.Parent
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local targetPosition = model:GetPivot().Position + Vector3.new(0, 0, 0)
local targetCFrame = CFrame.new(targetPosition)
local cframevalue = Instance.new("CFrameValue")
cframevalue.Parent = script
cframevalue.Value = model:GetPivot()

local tween = ts:Create(cframevalue, ti, {Value = targetCFrame})

wait(5)
	
while true do
	model:MoveTo(100, 0, 0)
	wait(5)
	tween:Play()
	cframevalue.Changed:Connect(function(val)
		model:PivotTo(val)
	end)
end

I’ve tried this code but it just keeps crashing my studio.

1 Like

Set the primary part of the model, then weld all other parts to the primary part, then you can tween the PrimaryPart and use the reverses boolean and repeat count in the Tween Info to reverse it and repeat.

2 Likes

I think its because of the model:MoveTo(100,0,0) line. Whats happening when the loop runs is:

  • Model teleports
  • Waits for 5 seconds (also I reccomend using task.wait() instead of wait() )
  • Plays your tween
  • Changes your model position based on the value
  • Goes back to the start of the loop

The problem is that your script isn’t waiting before going back to the start of the loop, I think adding a tween.Completed:Wait() will solve the problem.

This is the solution for the way you have it now, but I think @IDontCareAbout0129’s idea is a little more effective.

Try this code:

local model = script.Parent
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local targetPosition = model:GetPivot().Position + Vector3.new(0, 0, 0)
local targetCFrame = CFrame.new(targetPosition)
local cframevalue = Instance.new("CFrameValue")
cframevalue.Parent = script
cframevalue.Value = model:GetPivot()

local tween = ts:Create(cframevalue, ti, {Value = targetCFrame})
cframevalue.Changed:Connect(function(val)
		model:PivotTo(val)
end)
task.wait(5)
	
while true do
	model:PivotTo(CFrame.new(100, 0, 0))
	task.wait(5)
	tween:Play()
	tween.Completed:Wait()
end

I tried some stuff but i don’t know why this is happening.

local model = script.Parent
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local targetPosition = model:GetPivot().Position + Vector3.new(-362.752, 0, 0)
local targetCFrame = CFrame.new(targetPosition)
local cframevalue = Instance.new("CFrameValue")
cframevalue.Parent = script
cframevalue.Value = model:GetPivot()

local tween = ts:Create(cframevalue, ti, {Value = targetCFrame})

wait(5)
	
while true do
	model:MoveTo(Vector3.new(156.554, 0, 0))
	task.wait(0)
	tween:Play()
	
	cframevalue.Changed:Connect(function(val)
		model:PivotTo(val)
	end)
	
	tween.Completed:Wait()
end
External Media

1 - It’s supposed to go straight
2 - it’s supposed to go back inside the tunnel (the original position)
3 - it’s supposed to loop

local model = script.Parent
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

local originalCFrame = model:GetPivot()
local originalRotation = originalCFrame - originalCFrame.Position
local offset = Vector3.new(-362.752, 0, 0)
local targetPosition = originalCFrame.Position + offset
local targetCFrame = CFrame.new(targetPosition) * originalRotation

local cframeValue = Instance.new("CFrameValue")
cframeValue.Value = originalCFrame
cframeValue.Parent = script

-- Only connect once
cframeValue:GetPropertyChangedSignal("Value"):Connect(function()
	model:PivotTo(cframeValue.Value)
end)

while true do
	-- Tween to the target position (rotation preserved)
	local tween = ts:Create(cframeValue, ti, {Value = targetCFrame})
	tween:Play()
	tween.Completed:Wait()

	-- Instantly teleport back
	model:PivotTo(originalCFrame)
	cframeValue.Value = originalCFrame

	wait(1)
end

Well AI fixed my problem…

Oh, I see what it did. That went straight over my head that it was connecting each time. I’m glad its working now.

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