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