Help with NPC car moving

Hello!
I am trying to achieve something that makes cars on a highway spawn randomly e.g different lane, different car meshes

I want to make something similar to Highway Rush - Roblox
however, I have no clue how to make cars move like this

I would have thought they might have used something like

while wait(1) do
   car.CFrame = car.CFrame + CFrame.new(0,1,0)
end

But I want to try and figure out how to make it so that its not multiple scripts but, its just one script that controls all of the cars, any help would be appreciated, thanks!

PS: This is my 2nd post so if you could tell me if i have done anything wrong that would be appreciated!

1 Like

Use BodyVelocity, TweenService, and also CFrames ofcourse. There are many things you can use.

Condition -
If it’s not self-drivable, i.e. it needs to be drived by a player, then you need to mess with VehicleSeat. You can’t really depend on any of these.

And when it comes to your code, you can only add Vector to a CFrame.

It should be -

while wait(1) do
   car.CFrame = car.CFrame + Vector3.new(0,1,0)
end
1 Like

Hey, this is a good start from you, and it’s great that you want to make it optimized in one script. We will be using 2 scripts to make this, one server and one for the client.

Moving the cars:
First of all, in the game you sent, I assume that the cars move at different speeds, so give all your cars speed values. Now, instead of assigning each car a script with different speeds, we can just get the speed from the value. Next of all, make a server script, as you want the cars to move at the same location for all players. In this server script, you will move the cars using CFrame and not TweenService, as that would lag the server. In order to make the cars move smoothly, you can use a replicated event, to Tween the car to the new position from the old one, you will use the speed value from your car, to set the speed of the Tween, and you will set a new location for the car on the server when the Tween finishes. This way, the server will not be lagged out by roads full of cars, but they will also move smoothly at different speeds.

Spawning the cars:
This is very simple, just make an invisible part at the beginning of the road, and you can make another invisible part few studs on the front of this one, and when one car passes the 2nd invisible part, you spawn another car at the starting point. Once again, make sure to do this on the server.

By using the server scripts, exploiters cannot alter the position of the cars, and all players will see the cars in the same positions.

If you need some scripting help with this, let us know and me, and other dev forum members will try helping you.

2 Likes

Hello, I am kind of confused where you say

TweenService can be a heavy process on the server if we use too much of it at the same time, this is why we should use it on the client instead. On the server, the cars will teleport to the next location, on the client, they will “drive” / smoothly move, to the next location. Now in order to tell the client to move the car smoothly, we use replicated events/functions. This will send the needed information from the server to the client script. If you need an even more detailed explanation let me know.

Surely wouldn’t this ‘Spam’ remove events?

Like what if i had 50+ or 100+ cars on the road?

You can instead, create position values on the car, and add to the position value on the server, then get this value and tween it on the client.

You can also increase the amount of cars the player can see, and only tween the visible ones, then tween the other ones when they come to vision.

Isn’t this post simple ? I think there are already some good ways though.

How would I “add to the position value on the server”, Do I have to keep two different values like 1 value in the script and 1 value in the actual mesh?

No values are needed in the script, just in the mesh/model, lets’s say you can add 5 studs each time the car get’s to the next position on the server. Then tween the car to the new position.

So in the client script would you need something like this?

local TweenEvent = game:GetService("ReplicatedStorage").Tween
local TweenService = game:GetService("TweenService")

TweenEvent.OnClientEvent:Connect(function(car, tweenInfo, propertyTable)
	TweenService:Create(car, tweenInfo, propertyTable)
end)

I am quite new to tweening so I don’t understand a lot about it.

Using Client won’t update it to server.

Also, on the server-side of things, couldn’t we have a line of code something like this?

local TweenEvent = game.ReplicatedStorage.Tween

function tweenCar(car, Position, Speed)
	local properties = {
		Position = Position
	}
	
	local tweenInfo = {
		--(Speed / 60),			
		Speed, -- Time taken for a full animation
		Enum.EasingStyle.Quad,			-- Animation Style
		Enum.EasingDirection.InOut,			-- Animation Type
		0,									-- Number of repeats (-1 is infinite)
		false,								-- Reverse?
		1		
	}
	TweenEvent:FireAllClients(car,tweenInfo,properties)
end

I am using it each client instead of the server because:

Uh…Why it would even lag ? I don’t understand the logic.

Erm, not sure but I’m fairly new to tweening so I barely have any knowledge + I want to make it less laggy because the game I linked in the original post has laggy cars.

So use BodyVelocity instead. It’s fairly easy and any exploiter can even spam RemoteEvents, so you can even avoid it.

But if the car has collisions wouldn’t the car be able to get pushed around and go off track?

Just turnoff the collisions, and even tweening reacts with Physics I guess. That means even if something collides with a car while tweening, it can go offtrack.

RemoteEvents isn’t the best solution out there, just use BodyVelocity, then you’re good to go, and you’ll also remain server-sided and it won’t even lag.

Oh sorry btw cause I didn’t explain this enough I meant: couldn’t the NPC car get pushed around and not mine

I know remote events aren’t the best thing out there and there is more methods but I can’t really decide until I’ve fully tested them all.

I am also using remote events from server to client; not client to server so it shouldn’t be vulnerable to exploits.