Please read my first post about modulo.
I don’t know what that is though. Could you explain and give an example?
A different was to think of modules: We have a % n = r, where r is the remainder. Lua always uses the sign of n to determine the sign of r.
We add another value f: where f is an integer where f * n <= a, but is still the closest value to a when n > 0
(If a and n are both positive, then f would be the integer result of a / n)
Then a - fn = r Thus 17 % 5 → 17 - (3)5 → 17 - 15 = 2
-149 % 12 → -149 - (-13)12 → -149 - (-156) = 7
If n < 0 then f * n >= a
-6 % -4 → -6 - (1)(-4) → -6 - (-4) = -2
Also 120 % -7 → 120 - (-18)(-7) → 120 - 126 = -6
tl;dr:
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
So how could I implement it in the code?
That I think, is a fine job for you yourself to do.
I’ll help you a bit:
local roadPlacementCount = 0;
...
event...
roadPlacementCount += 1
...
if roadPlacementCount % 4 == 0 then
previousRoad:Destroy()
end
event end...
i made something like this before, just check if its the first time, if it is clone and move it like 25 studs for example, and move the outbound part 25 studs too, and check the first time, if its not the first time then clone the recently cloned model and move it 25 studs again.
then, your result should be like this:
https://www.roblox.com/games/6997073541/infinite-rooms
you might wanna add the fog if you dont want them to see the sky
I tried doing it myself with a touch event , but it just gave me an error. How can I get the previous road?
local roadModel = game.ReplicatedStorage.Road
local roadCFrame = workspace.Roads.Road:GetPivot()
game.Players.LocalPlayer.CharacterAdded:Wait()
function clone()
if (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - roadCFrame.Position).Magnitude > 1 then
local newRoad = roadModel:Clone()
newRoad:PivotTo(roadCFrame * CFrame.new(0,0,-workspace.Roads.Road.Hitbox.CFrame.Z))
roadCFrame = newRoad:GetPivot()
newRoad.Parent = workspace.Roads
newRoad:Destroy()
end
end
game.ReplicatedStorage.MoveRoad.OnClientEvent:Connect(function()
game:GetService("RunService").Heartbeat:Connect(function()
for _,v in pairs(workspace.Roads:GetChildren()) do
v:SetPrimaryPartCFrame(v.Hitbox.CFrame + Vector3.new(0,0,1))
end
local roadPlacementCount = 0;
local previousRoad
clone()
if roadPlacementCount % 4 == 0 then
previousRoad:Destroy()
end
end)
end)
I suggest adding a variable way up which takes ahold of a reference of the last road
It seems to skip every other road.
local roadModel = game.ReplicatedStorage.Road
local roadCFrame = workspace.Roads.Road:GetPivot()
local roadPlacementCount = 0;
game.Players.LocalPlayer.CharacterAdded:Wait()
function clone()
if (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - roadCFrame.Position).Magnitude > 1 then
local newRoad = roadModel:Clone()
newRoad:PivotTo(roadCFrame * CFrame.new(0,0,-workspace.Roads.Road.Hitbox.CFrame.Z))
roadCFrame = newRoad:GetPivot()
newRoad.Parent = workspace.Roads
return newRoad
end
end
game.ReplicatedStorage.MoveRoad.OnClientEvent:Connect(function()
task.spawn(function()
game:GetService("RunService").Heartbeat:Connect(function()
for _,v in pairs(workspace.Roads:GetChildren()) do
v:SetPrimaryPartCFrame(v.Hitbox.CFrame + Vector3.new(0,0,1))
end
end)
end)
while true do
task.wait(5)
local previousRoad = clone()
roadPlacementCount += 1
if roadPlacementCount % 4 == 0 then
previousRoad:Destroy()
end
end
end)
local previousRoads = {}
...
table.insert(previousRoads, road)
...
for _,v in pairs(previousRoads) do
v:Destroy()
end
I have done this before with an infinite hallway. It is pretty simple, all you have to do it clone the object and using the primary part to move it forwards as long as the model is. And you activate this when they touch the road it dupes once so that it doesn’t lag you too much and then you can just add fog or something.
Here is the script I used (I put the script inside the part that acts as the ground that you touch):
script.Parent.Touched:Connect(function(hit)
print(hit.Name)
if hit.Parent.Humanoid then
local newHallway = script.Parent.Parent:Clone()
newHallway:SetPrimaryPartCFrame(CFrame.new(newHallway.PrimaryPart.Position + Vector3.new(149, 0, 0)))
newHallway.Parent = game.Workspace
script:Destroy()
end
end)
As you can see, I did exactly what I said, and the hallway is 149 studs long so I move it that much further once I clone it.
Hope this was helpful!
Edit: Make sure to replace some things with your values and objects if needed!
How can I delete the ones I’ve already passed?
Why would you want to do that? Just wait a few minutes, I am going to make a script for you!
So I don’t create too many road pieces and it starts lagging.
What does your road model look like, because if it’s only a few parts you would have to be walking for years to start lagging. Also if you remove the road behind you, how will anyone else go over it. Unless you want it like a rendering system like Minecraft or something?
Yeah, like a rendering system. All players will be on a car that stay still, but the road will be moving backwards. It will give it that illusion that the car is moving.
So you want a moving road and your not moving? I feel like it would just be easier to have the player move. What are you going to use this for?
It’s like a hangout game. All of the players are on a car, on a “road trip”.
So all of the players are going to be together or apart? Because if they are together it will make it a lot easier to script.
They’re going to be together, standing on top of a car.