# Random tile generation

Hello. I am building a track runner game (Like temple run). I am having some problems with the generation script. I don’t know the logics behind this type of generation. Everything generates on the wrong place. This is my code:

``````local Track = game.ServerStorage.Track
local Position = CFrame.new(0,0,0)

for i = 0, 100 do
local items = Track:GetChildren()
local TrackPart = items[math.random(1, #items)]:Clone()
TrackPart.Parent = game.Workspace
TrackPart:SetPrimaryPartCFrame(Position)
if TrackPart.Name == "Left" then
Position = Position + Vector3.new(25, 0, 0)
Position = Position*CFrame.Angles(0, math.rad(-90), 0)
end
if TrackPart.Name == "Right" then
Position = Position + Vector3.new(-25, 0, 0)
Position = Position*CFrame.Angles(0, math.rad(-90), 0)
end
if TrackPart.Name == "Straight" then
Position = Position + Vector3.new(25, 0, 0)
end
print(Position)
end
``````

What can i change to make it generate right?

4 Likes

Scroll down to the math operations on this wiki article CFrame | Documentation - Roblox Creator Hub

You can’t add 2 CFrames together, only translate the position by a Vector3.

How would i change the rotation then?

Try multiplying them together, see if that gives a good result. Not sure though.

you may have to do a seperate calc using cframe.angles or something like cframe.new(0,0,0) * cframe.angles. which would change the angle section of the cframe and leave the position part

for example
`CFrame.new(1,2,3)*CFrame.Angles(0,math.rad(90),0)`
this would rotate the cframe but keep it in the same position i think anyway

How do add this on top of the old CFrame then?

on what line. Is it the one you got the error one so line 10 or no

I get errors on all the lines that changes Position (The CFrame).

For some reason when i do this the models teleport to like -30102824737 on the y axis.

Never mind, i fixed it myself.

I changed the variable.

How did you fix it? Incase anyone else has a similair problem

are you rotating the models CFrame and doing something like this

this works for me i just tested it

I made the thing generate but they still don’t generate in the right place. Can you help me with the logic?

Yea sure can I see your code that you need help on and ill try help

I updated the code in my post so just scroll up. Thanks.

Can you send me what happens when you run this script and it may help me further

sorry for all this but what is it purposed to look like try building it and this will be the last thing ill need

One thing wrong with your code is that the right and left code both rotate by -90 degrees. You should make the right one 90 instead of -90. You also should use lookVector and right vector to align with the updated frame instead of 3d world space. Once the track rotates to the left, your “Straight” is actually right. With these changes, your code looks like this:

``````local Track = game.ServerStorage.Track
local Position = CFrame.new(0,0,0)

for i = 0, 100 do
local items = Track:GetChildren()
local TrackPart = items[math.random(1, #items)]:Clone()
TrackPart.Parent = game.Workspace
TrackPart:SetPrimaryPartCFrame(Position)
if TrackPart.Name == "Left" then
Position = Position - (Positon.rightVector * 25)
Position = Position*CFrame.Angles(0, math.rad(-90), 0)
end
if TrackPart.Name == "Right" then
Position = Position + (Position.rightVector * 25)
Position = Position*CFrame.Angles(0, math.rad(90), 0)
end
if TrackPart.Name == "Straight" then
Position = Position + (Positon.lookVector * 25)
end
print(Position)
end
``````

This works in my demo, however I have yet to see what your object hierarchy looks like (the workspace in the explorer tab).

I breezed through the explanation a bit. If you have more specific question about how this all works, I’m happy to answer them.