Broken Spawn system

hello! We have a script that transfers a vehicle from ServerStorage to Workspace but when spawned it is either anchored or the scripts are disabled. In the script it even has a line to make it unanchored but it still doesn’t work.

We would like to try to make the code work to make the train unanchored or let the vehicle move instead of it being frozen.

local class153 = game.ServerStorage.Trains.test153:Clone()


    script.Parent.MouseButton1Click:Connect(function()

        class153:SetPrimaryPartCFrame(CFrame.new(642.516, 34.037, 645.331))
        class153.Parent = workspace
        wait()
        class153.Anchored = false

    end)

thats a model u cant anchor/unanchor models but u can anchor/unanchor parts

Do you know a way of making it unanchored without unanchoring each part in the model individually?

I would like to ask @BanTech to give me an insight on how they spawn a train (if they use replicated storage)

In terms of your scripts, have them enabled (not disabled) the whole time. They can’t run in the storage folders so there’s no need to mess around enabling or disabling them.

In terms of anchored parts, we have every part in the train anchored. Before unanchoring, we create welds between each part of each carriage to a central root part. You can use WeldConstraint for this, or create a Weld instance. Once done, we unanchor it.

The general principle looks like this:

local train = class153 -- we have one script to do all functionality so this is set by what you select on the GUI

for _, carriage in ipairs( train:GetChildren() ) do
    if not carriage:IsA( 'Model' ) then
        continue
    end
    local root = carriage:FindFirstChild( 'CarriageRoot' )
    -- IMPORTANT create a part called CarriageRoot in each carriage or replace with a part you want to weld to
    for _, descendant in ipairs( carriage:GetDescendants() ) do
        if not descendant:IsA( 'BasePart' ) or descendant ==  root then
            continue
        end
        local weld = Instance.new( 'WeldConstraint' )
        weld.Part0 = root
        weld.Part1 = descendant
        weld.Parent = descendant
        descendant.Anchored = false
    end
    root.Anchored = false
end

You can do other things here too, so for example we set up stuff with the lights, we sort out the seats, we make sure everything has the right physical properties, etc. Using a loop like this makes it pretty easy, and in your case you’d pop it in where you current Anchored = false line is.

If your trains have a different hierarchy where carriages aren’t models or they aren’t direct children of the train model then obviously edit those appropriately. This is just to give you a general idea of how it works.

3 Likes

Thanks! This will be great help and a great insight to how to make it work! I’ll look into it!