✓ Accepted answer
Got in contact with OP over discord.
He wanted to make a car move along an endless road, but instead of moving the car, he wants to move the road. Visual he sent me:
The code below creates sections of road to make it appear as if a car is driving on an endless road. When the car gets near the end of the last road section, it creates a new road and adds it to a list of road sections. The script makes sure there are only two roads in game at a time.
you can test the game here
local car = workspace.DMC12
local roadModel = game:GetService("ReplicatedStorage").Road
local ROAD_MOVE_INCREMENT = 1
local roads = {}
function createRoad(cframe)
local road = roadModel:Clone()
road.Parent = workspace
cframe = cframe or road.PrimaryPart.CFrame
road:SetPrimaryPartCFrame(cframe)
table.insert(roads, road)
return road
end
local road = createRoad()
while true do
wait()
if #roads > 2 then
local roadToDestroy = roads[1]
table.remove(roads, 1)
roadToDestroy:Destroy()
end
for _, road in pairs(roads)do
road:SetPrimaryPartCFrame(road.PrimaryPart.CFrame * CFrame.new(0,0,-ROAD_MOVE_INCREMENT))
end
local farthestRoad = roads[#roads]
local roadMiddlePosition = farthestRoad.PrimaryPart.Position.Z
local carFrontPosition = car.PrimaryPart.Position.Z + car.PrimaryPart.Size.Z/2
if carFrontPosition >= roadMiddlePosition then
createRoad(farthestRoad.PrimaryPart.CFrame * CFrame.new(0,0,farthestRoad.PrimaryPart.Size.Z))
end
end
4 comments
it does not return
Why does it need to return? What is your end goal ?
What are you trying to return?
I'm thinking maybe we wants the loop to not run at all when it's not being used and be recreated again on touch?? Best to know what he wants the code to do though