I need help making an infinite road [NO LONGER WORKING ON]

Hey, I’m no longer working on this project. If you’re looking here for help on other games, just look at the replies. I’ve given up a week ago.

VVVVVVV OLD POST VVVVVVV

Hello! I’m trying to create an infinite road that scrolls past you as the game goes on, i wanna create something similar to it in this game:

Game

An Infinite Road Trip

I’ve got an idea for what i want, i’ll summarize it here;

If you don’t know what i mean, i can describe it if you reply.

I really need help figuring out how to script this! Help is very much appreciated!

2 Likes

Based on your game’s thumbnail, I assume you’re using fog to make the front and back of the roads fade out which is a good idea

The code I’d use is pretty simple. I would, first of all, set it on the client in a LocalScript in StarterPlayerScripts or something

Let’s say that when the player travels along the X axis long enough, the new roads appear. Let’s also say that the roads are 300 studs long each (they can be as long as you want, this script won’t care, it’ll do the necessary math to make sure the roads all fit)

We can determine how far the player is from the center of a road at any point using Magnitude. Since we’re only worried about the X axis, we’ll only need to worry about the X values of the roads’ and player’s positions

I also believe a while loop is best here

local road = script.Road
local start = workspace:WaitForChild("StartPart")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()

local roadGroup = {} -- road1, road2, road3. road2 is middle
while wait(1) do -- new roads won't be made constantly, this should suffice
	if #roadGroup == 0 then -- create the roads if they don't exist yet
		for roadNum = 1, 3 do
			local newRoad = road:Clone()
			newRoad.Name = "Road" .. tostring(roadNum)
			newRoad.Parent = workspace
			newRoad.CFrame = start.CFrame * CFrame.new((roadNum - 2) * newRoad.Size.X / 2, 0, 0)
			table.insert(roadGroup, newRoad)
		end
	else
		local playerPos = Vector3.new(character.HumanoidRootPart.Position.X, 0, 0)
		local roadPos = Vector3.new(roadGroup[2].Position.X, 0, 0)
		local dist = (playerPos - roadPos).Magnitude
		if dist > roadGroup[2].Size.X / 2 then
			local road1 = roadGroup[1]
			local road2 = roadGroup[2]
			local road3 = roadGroup[3]
			
			local distToRoad1 = (character.HumanoidRootPart.Position - road1.Position).Magnitude
			local distToRoad3 = (character.HumanoidRootPart.Position - road3.Position).Magnitude
			if distToRoad1 > distToRoad3 then
				roadGroup[2] = road3
				roadGroup[1] = road2
				roadGroup[3] = road1
				road1.CFrame = road1.CFrame * CFrame.new(road1.Size.X * 2, 0, 0)
			else
				roadGroup[2] = road1
				roadGroup[3] = road2
				roadGroup[1] = road3
				road3.CFrame = road3.CFrame * CFrame.new(road1.Size.X * -2, 0, 0)
			end
		end
	end
end
1 Like

I’m not talking about the character moving and the road generating in front, i’m talking about the road looping as i showed in the image.
The road hould scroll past the player, giving the illusion that the player is actually moving forward

in fact i dont really think it should involve the character at all.

also, the game i linked is NOT my game. but the fog idea is good.

2 Likes

In that case, it’s basically the same principle. Build off of the code I gave you. You’re still shuffling the positions of the roads in a loop like I wrote for you. Ignore the code where it constantly tracks the player’s distance from the center road and make use of TweenService to smoothly move the roads (preferably on the client to ensure smooth gameplay)

I would use TweenService say, every 2 seconds on all of the roads to move them road.Size.X studs in the X direction via CFrame and then move the farthest road to where the first road used to be and update the roadGroup array like in the old loop.

And don’t delete the road and clone it again. That’s just dumb. All you have to do is move it instantly back to the start by setting the CFrame. The player won’t tell any difference

1 Like

Why not just put a teleport at the end of the road to teleport you back to the start. It would make it seem like it’s an infinite road.