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 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.