Move multiple parts with script

G’day, I made a game about a train and I need help with the landscape.
I have to make the landscape move, I built the mountains, the trees and the rails, but I can’t get them to move. I have tried welding them together but the script only makes the ground move and I can’t make a model because they are meshes. I saw a lot of games that work like this, and I’ve searched for how to do it on the Internet like millions of times.

does anyone have an idea?

4 Likes

Yeah you can, it works the same with MeshParts and normal Parts.

Are you keeping the landscape anchored? It should work just fine if you put all the MeshParts into the same Model and use SetPrimaryPartCFrame. Something like this:

local model = --
local velocity = Vector3.new(20, 0, 0)

while true do
    local dt = wait()
    model:SetPrimaryPartCFrame(
        model.PrimaryPart.CFrame + velocity * dt
    )
end

Of course make sure you set the PrimaryPart of the Model.

1 Like

it works thanks, but after reaching a certain distance the model has to teleport back. how can I do it?

1 Like

You could make three copies of the landscape and place them front-to-back. The player should always be in the middle copy, and every time they cross the border to the front or back copy, all three copies move over by exactly the length of a copy to keep the player in the middle one.

Here’s some example code that should work more or less:

local character = --
local landscapeModel = --
local landscapeLength = 100 --Tune this to make it so there's no gap between the copies
local velocity = Vector3.new(20, 0, 0)

local scrollingLandscape = Instance.new("Model")
scrollingLandscape.Name = "Scrolling Landscape"

--Setup landscape copies
for i = -1, 1 do
    local landscapeModelCopy = landscapeModel:Clone()
    landscapeModelCopy:SetPrimaryPartCFrame(CFrame.new(i * landscapeLength, 0, 0))
    landscapeModelCopy.Name = landscapeModelCopy.Name .. ("copy %d"):format(i)

    landscapeModelCopy.Parent = scrollingLandscape
    if i == 0 then
        --Allows moving all three copies as if they were "stuck" to the middle copy, just by moving the scrollingLandscape
        scrollingLandscape.PrimaryPart = landscapeModelCopy.PrimaryPart
    end
end

while true do
    local dt = wait()
    scrollingLandscape:SetPrimaryPartCFrame(
        scrollingLandscape.PrimaryPart.CFrame + velocity * dt
    )
    
    --Move the overall landscape to keep the character in the middle copy
    local translation = (scrollingLandscape.PrimaryPart.Position - character.PrimaryPart.Position).X
    --Floor to nearest whole number of lengths
    local newTranslation = math.floor(translation / landscapeLength) * landscapeLength
    scrollingLandscape:SetPrimaryPartCFrame(CFrame.new(newTranslation, 0, 0))
end

Haven’t tested so it might not work, but maybe the idea makes sense?

Yeah, it might have sense. In any case I solved the problem by using “SetPrimaryPartCFrame” another time, to make sure that the landscape is transported back, thus creating an infinite loop.
Thanks

1 Like