Need help with making an infinite road

Hello. I’m trying to make an infinite road, but I don’t just want to clone it over and over. I want to clone it until it gets to the horizon, then when it moves back (the road moves, not the car. it’s an illusion) I want to delete the part at the back and add a new one at the front. Here’s the code that I have now.

local road = workspace.Roads.Road
local rs = game:GetService("RunService")
local driving = true

wait(2)

rs.Heartbeat:Connect(function()
    road:SetPrimaryPartCFrame(road.Hitbox.CFrame + Vector3.new(0,0,0.5))
end)

Any thoughts?

4 Likes

I am trying to understand what you mean by “moving the road”. If a player pressed the keybind for the car to go forward, the road would move backwards, locally, on the players client?

This approach would work, if you only want the local player to see the road move. In any other case, like doing the moving on the server, this won’t work.

Feel free to correct me, if I’m wrong.

No. The car isn’t controlled. The road moves backwards for the illusion that the car is moving forwards.

I don’t really see a situation where this is the best solution. For any kind of illusion to work you’d have to also move the scenery surrounding the vehicle - and even then I would expect some stutter. If you are set on doing this, however, I think the best way to do it would be to have the road between horizons split into multiple parts. A few parts would be within the field of view and the others would be outside. The connected parts could then tween forwards with the foremost parts being deleted when they pass the horizon/field of view and a clone being added to the back of the road. You could probably also weld the whole structure to a single piece which is also destroyed/remade when it reaches the end. This would allow you to only repetitively tween a single part.

If you really just wanted an illusion you could probably get away with making a large circular road with scenery which you tween around a fixed position.

Yes, but I don’t know how to script it. That’s why I’m asking for help.

The rotation idea just requires a basic idea of Tween . You would just have to weld a map/road to a central part which you repetitively tween 360 degrees.

I wrote a basic script for the other idea. It’s not well optimised but it works and the logic is what matters here. The colour changes are purely cosmetic to show how it works better. I also made it so the road travels along the z axis, but that can easily be changed/edited.

Workspace hierarchy:

ReplicatedStorage hierarchy:
Screenshot 2021-08-21 at 17.31.59

1 Like

Hello is it possible you could give an rbxl file to this as I dont really know where to position EndPart StartPart and what properties i should change to the RoadParts?

I’ve made the original model available here: Road - Roblox

But I’d absolutely suggest using the code as a rough guide at best in developing a similar mechanism for any real use in-game. In retrospect, not very performant code.

1 Like

LIFE SAVER! <33 you’re the best dude