How can I make my CFrame movement code non-hardcoded?

(This code segment is concise, so there is no need for me to put a file in here, instead, I will be posting the screenshot of the code in its entirety)


Essentially, what this code will do is move baseplates in a linear direction to make it appear as if the player (who is in a helicopter) is moving.

I wanted to add a treeline and foliage to the map, so it looks better; however I cannot correctly get the treelines to join the baseplate, so I have concluded I could make scripts for these elements to get them to move as well.

The issue I have with this code is that I have a specific location for each baseplate (, -10, n)), and I mostly have to change the location values for each baseplate since there are 3 in different locations. Is there any way I can make the location values universal for each object with this script in it? (i.e., if I were to put this script into another part, it would move forwards in its current location without entering location values for each part)

You can use :SetPrimaryPartCFrame(), grouping it as a model and then setting the baseplate as the primary part.

1 Like

@Procedurall gave a pretty good answer, so now I’m going to review your code. This is Code Review, after all!

  1. Why are you doing while i == 1 do? ROBLOX will have to look for i, check if it’s equal to 1, then run the code in the loop. Instead, you can do while true do, and it’ll be faster(not by much).

  2. What use is h? It doesn’t look like you’re using it anywhere in your code.

  3. wait(0.01) is no different from wait(), since 0.03 is the minimum parameter. Oh, and also I’m pretty sure you should avoid using it without any parameter: Avoiding wait() and why.

Hope I helped! If I made any mistakes, please tell me.

1 Like

Where exactly should I used :SetPrimaryPartCFrame()? I am new to using CFrames so I am not exactly sure how I should implement it.

Thank you, I forgot about while true do , that is very helpful. I forgot to remove h from the code, I was originally going to use that as a variable for something else, but I mostly got rid of that idea, so thank you for telling me about that. Also, thank you for the article about wait() because I have been using that method for a very long time, but now I don’t think I will.

Instead of manually moving it like this and hoping that it lines up with the helicopter, you could just set it to some position relative to the helicopter every cycle of the loop. Something like this:

local movingBrick = workspace.Baseplate3
local helicopter = workspace.Helicopter --replace with the actual path to the heli
local startPosition =, -10, 0)

while true do
	local z = helicopter.PrimaryPart.Position.Z
	movingBrick.CFrame = +, 0, z))