TweenService Part

Hello, I’m trying to make a script at the moment that lets a part run over a track with the help of Tweenservice, unfortunately the part behaves a bit strangely during the rotation.

Here is the script how it works but the part is not smooth:

--Coaster Settings
local Speed = 10

local folderName = "Curve"

local workspace = game:GetService("Workspace")

local folder = workspace:FindFirstChild(folderName)
if not folder then
	return
end

local function sortBySpineNumber(parts)
	table.sort(parts, function(a, b)
		local spineNumberA = tonumber(a.Name:match("%d+$"))
		local spineNumberB = tonumber(b.Name:match("%d+$"))
		return spineNumberA < spineNumberB
	end)
end

local parts = {}
for _, part in ipairs(folder:GetChildren()) do
	if part:IsA("Part") and part.Name:match("^Spine:%d+$") then
		table.insert(parts, part)
	end
end

sortBySpineNumber(parts)

local movingPart = Instance.new("Part")
movingPart.Name = "MovingPart"
movingPart.Anchored = true
movingPart.Size = Vector3.new(4,4,4)
movingPart.CanCollide = false
movingPart.Parent = workspace

local TweenService = game:GetService("TweenService")

local function moveOverSpineParts(speed)
	for i = 2, #parts, 2 do
		local part = parts[i]

		local partPosition = part.Position
		local distance = (partPosition - movingPart.Position).Magnitude
		local tweenInfo = TweenInfo.new(distance / speed, Enum.EasingStyle.Linear)
		local tweenPosition = TweenService:Create(movingPart, tweenInfo, {Position = partPosition})

		local partRotation = part.Rotation

		tweenPosition:Play()
		movingPart.Rotation = partRotation

		tweenPosition.Completed:Wait()

	end
end

moveOverSpineParts(Speed)

Video:


And here’s the part when things get weird:

local function moveOverSpineParts(speed)
	for i = 2, #parts, 2 do
		local part = parts[i]

		local partPosition = part.Position
		local distance = (partPosition - movingPart.Position).Magnitude
		local tweenInfo = TweenInfo.new(distance / speed, Enum.EasingStyle.Linear)
		local tweenPosition = TweenService:Create(movingPart, tweenInfo, {Position = partPosition})

		local partRotation = part.Rotation
		local tweenRotation = TweenService:Create(movingPart, tweenInfo, {Rotation = partRotation})

		tweenPosition:Play()
		tweenRotation:Play()

		tweenPosition.Completed:Wait()
	end
end

Video:

Does anyone know how I can do that with tween so that the part comes through the curves smoothly without it spinning so weirdly.

I hope you can help me :slight_smile:

its because it collides with the rails , make the rails collisionless

I already have it doesn’t work anyway ):

Why not just use a single tween and alter the CFrame of the part you are moving?

How do you mean that? I want it to come smoothly around the curves so that it doesn’t look so hard when it’s eg. directly is changed

I just mean you can tween the parts CFrame and change the rotation and position at the same time. Or have a single goal containing the end position and rotation at each step and create one tween and play it. Playing another tween whilst a tween is already running will cancel the first tween so will give that undesired behaviour.

1 Like

I’ve done it like this now it works to some extent sometimes it still spins weird do you have an idea how to fix it?
Script:

		local partPosition = part.Position
		local distance = (partPosition - movingPart.Position).Magnitude
		local tweenInfo = TweenInfo.new(distance / speed, Enum.EasingStyle.Linear)
		local tweenPosition = TweenService:Create(movingPart, tweenInfo, { Position = partPosition })

		local startRotation = movingPart.Orientation
		local endRotation = part.Orientation
		local rotationTweenInfo = TweenInfo.new(distance / speed, Enum.EasingStyle.Linear)
		local tweenRotation = TweenService:Create(movingPart, rotationTweenInfo, { Orientation = endRotation })

		tweenPosition:Play()
		tweenRotation:Play()

‘’’

		local partPosition = part.Position
		local distance = (partPosition - movingPart.Position).Magnitude
		local startRotation = movingPart.Orientation
		local endRotation = part.Orientation
		local goal = {}
		goal.Position = partPosition
		goal.Orientation = endRotation
		local tweenInfo = TweenInfo.new(distance / speed, Enum.EasingStyle.Linear)
		local tween = TweenService:Create(movingPart, tweenInfo , goal)

		tween:Play()

Now the part goes straight to the end and stays there

what if you remove the rails completely (or make them can collide can query can touch off)

No it has nothing to do with it even if I disable all and change colliongroups. It has to do with tween

umm maybe try to space out the tween blocks , can u give me a place file so we can try fix

I managed it anyway thanks for the time and the try (:

1 Like