How do I make a car that moves and then teleports back to that spots and moves as a loop

So I’m Working on a game and I want a car to come out of a tunnel and drive to the other tunnel
– and it just goes straight btw to the other side–

So I want to make a car that drives from point B to Point A * note is a one way street *

My grammar is bad since I did this in a rush

3 Likes

You can build a looping lerp function pretty easily. The idea behind it would be you take the current time, take the modulus based on the duration of this loop, and feed that modulus value into the lerp. Here’s an example:

local RunService = game:GetService("RunService")

local car = some.instance -- for the purposes of this post, it is assumed to be a Model
local loopDuration = 123 -- some number of seconds for the loop duration to take place
local pointA = Vector3.new(1, 2, 3) -- up to you
local pointB = Vector3.new(4, 5, 6) -- up to you

function calculateLerpedCFrame(t, pointA, pointB)
    local displacement = pointB - pointA
    if displacement.Magnitude == 0 then -- pointA and pointB are identical
        return pointA
    end

    local direction = displacement.Unit

    local lerpedPosition = pointA + (displacement * t)
    local lerpedCFrame = CFrame.new(lerpedPosition, lerpedPosition + direction)

    return lerpedCFrame
end

function calculateLoopedLerpValue(timestamp, duration)
    return timestamp % duration
end

function onStepped()
    local currentDateTime = DateTime.now()
    local timestamp = currentDateTime.UnixTimestampMillis / 1000 -- put it into seconds with rough millisecond accuracy
    local lerpParameter = calculateLoopedLerpValue(timestamp, loopDuration)
    local carCFrame = calculateLerpedCFrame(lerpParameter, pointA, pointB)

    car:SetPrimaryPartCFrame(carCFrame)
end

RunService.Stepped:Connect(onStepped) -- or use one of the new run service signals
  • disclaimer - I wrote this all just in Chrome so I may have missed this up a bit.
2 Likes

I have no idea what it means since I’m trash at script.

I meant this "new run service signals’

Pretty much the script that Post 2 sent is basically Lerping the Car

Lerping pretty much takes an end position (Provided that you already have a start position to get a direction result) and a range between 0 & 1, it’ll return back the Lerped CFrame I believe

Although I would try this, it would be confusing to other members

Another way you can try is by assigning a BodyVelocity object to the Car itself for it to move from Point A to Point B that way, then detect when the Car ends by detecting a Touched event for Point B

1 Like

how would you make it so its not a loop, and only lerps in a random amount of time?
for example, this script:

local TweenService = game:GetService("TweenService")
local PlaneLobby = script.Parent
local tweenInfo = TweenInfo.new(
	23.448, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	true, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween = TweenService:Create(PlaneLobby, tweenInfo, {Position = Vector3.new(-345.317, 58.508, -260.597)})

while true do
	local n = math.random(30, 120)
	wait(n)
	print(n)
	tween:Play()
	PlaneLobby["Plane Sound"]:Play()
end

it waits a random amount of time, then goes to its vector 3 position