I’m trying to tween a model of a car with TweenService and I just need to find a way to make it tween to multiple places but the code below is where I am at so far and I am kind of lost right now.
If anyone has any suggestions of how to move a model without using tweens like any other better ways to do what my goal is that would help too.
local TweenService = game:GetService("TweenService")
local Car1 = game.Workspace.L5RedCarTween1
local Car1Primary = Car1.PrimaryPart
local goal1 = {CFrame = CFrame.new(-45.352, 547.143, 51.816)}
local goal2 = {CFrame = CFrame.new(-4.674, 547.143, 54.704)}
local tweeninfo = TweenInfo.new(
5,
Enum.EasingStyle.Quint,
Enum.EasingDirection.In,
0,
false,
3
)
local tween1 = TweenService:Create(Car1Primary, tweeninfo, goal1)
local tween2 = TweenService:Create(Car1Primary, tweeninfo, goal2)
while true do
tween1:Play()
tween1.Completed:Connect(function()
task.wait(3)
tween1:Cancel()
tween2:Play()
end)
task.wait(3)
tween2.Completed:Connect(function()
--tween2:Remove()
end)
task.wait(3)
end
I think using TweenService is a good approach.
I would do it like this:
A main tween function to receive the target position, create the goal data after receive the position, create the Tween track, play it, wait for Tween.Completed and from there call the whole function again sending the new target position.
The list of target positions could be an array/table of positions, and each time the main tween function is called, increase a key number to get from that table.
So first time main tween function runs, select the key 1, after tween complete, that variable key is increased to 2 and when tween function runs again it gets the second target position from table and so on
Like what @Dev_Peashie said, you would need to make a function with the parameters of the target position, then return the created tween, so you would be able to listen for events. Then you would just need to loop over that array that contains the positions and call the function.
local function CreateTween(targetCFrame: CFrame): Tween
local tween = TweenService:Create(Car1, tweeninfo, { CFrame = targetCFrame })
tween:Play()
return tween
end
local listOfCframes: { CFrame } = {
CFrame.new(-45.352, 547.143, 51.816),
CFrame.new(-4.674, 547.143, 54.704),
...
}
while true do
for _, cframe in ipairs(listOfCframes) do
local tween = CreateTween(cframe)
tween.Completed:Wait()
task.wait(3)
end
end
Do you know if there is any other way to make a car look animated in-game like is there a way to actually animate it because tween just looks choppy and is confusing to script an animated car with.