# Help with Random Tunnel Generation

Hey guys
Recently Ive been trying to make a tunnel generate with turns and curves randomly. I have the models, but for the prototype im using parts.

FYI I have been scripting since 2 years but I quit 4 months ago and hence have forgetten most of it. Help me revive my memory!

This is the code

``````local count = 0
local lastPart = workspace.Part
local tunnels = game.ServerStorage.Folder:GetChildren()
local rotationOffset = Vector3.new(0,0,0)

while count < 50 do
count += 1
local p = tunnels[math.random(1, #tunnels)]:Clone()
if p:FindFirstChild("C") and p.C.Value == true then
rotationOffset += Vector3.new(0, 90, 0)
elseif p:FindFirstChild("C") and p.C.Value == false then
rotationOffset += Vector3.new(0, -90, 0)
end
local offset = Vector3.new(lastPart.Size.X/2 + p.Size.X/2, 0, 0)
if rotationOffset.Y ~= 0 then
p.Position = lastPart.Position + Vector3.new(0, 0, offset.X)
else
p.Position = lastPart.Position + offset
end
p.Orientation += rotationOffset
lastPart = p
p.Parent = workspace
print(rotationOffset)
end
``````

I want it to look like this

but its something like this

How do I fix it? As I said earlier if i just get a bit of explaination i might be able to do it easily, i just need someone to click my head due to my past experience.

1 Like

First off you could move the p.C.Value check below and do something like:

if p:FindFirstChild(âCâ) then
if p.C.Value == true
â do code
elseif p.C.Value == false
âdo other code
end

I just tested it and the rotation is fine it just doesnât line up

Thats what i was saying. I need it to line up

It looks like youâre putting âoffsetXâ into the Z? Also seems there are not enough ifs to determine if itâs rotated left or right.

Suggestion- take out the ârandomâ and hard-code a short sequence of straights and corners in a table. Physically move the parts where the script should put them and look at each of their numeric transforms, then compare to what your scriptâs doing.

1 Like

Here, I just subtracted 1 and 3 from the offset and it works fine, turning should be improved though.

``````local count = 0
local lastPart = workspace.Part
local tunnels = game.ServerStorage.Folder:GetChildren()
local rotationOffset = Vector3.new(0,0,0)

while count < 50 do
count += 1
local p = tunnels[math.random(1, #tunnels)]:Clone()
if p:FindFirstChild("C") and p.C.Value == true then
rotationOffset += Vector3.new(0, 90, 0)
elseif p:FindFirstChild("C") and p.C.Value == false then
rotationOffset += Vector3.new(0, -90, 0)
end
local offset = Vector3.new(lastPart.Size.X/2 + p.Size.X/2, 0, 0)
if rotationOffset.Y ~= 0 then
p.Position = lastPart.Position + Vector3.new(offset.X - 1, 0, offset.X - 3)
else
p.Position = lastPart.Position + offset
end
p.Orientation += rotationOffset
lastPart = p
p.Parent = workspace
print(rotationOffset)
end
``````
1 Like

If I was making this I might just leave rotation out of it and make 6 tiles (or 8 if the straights are directional) and place them on a square grid in world space. Then based on which model you just put down and the next direction you know the next model.

You still have to decide what happens when the tunnel randomly turns back and runs into itself.

1 Like

Thanks @onboquo for the suggestions, but since the tunnels actual model is curved it might leave gaps between. I will definitely try out your suggestions tho!

@temporaryacount101 did u just randomly subtract 1 and 3 and it worked? If possible please explain me why it works, i want to learn instead of just making it work.

It just does this.

Also I said, I donât need hand written code, I need to know what to do and Iâll do it myself.