Why does my train's PrimaryPart destroy itself once I clone the train to Workspace?

I’m making a turn-back system for my train, and for my method, I need to change its CFrame. For that, I need to change the primary part’s CFrame, but for some reason, whenever the train is cloned to Workspace (to spawn), I discovered that it gets destroyed and the script errors.

I have no idea why this is and the only script in the game that actually references the train this early is this one:

local clonedTrain = trains[trainName]:Clone()

clonedTrain.Parent = workspace.ActiveTrains
clonedTrain:PivotTo(sidings[siding].SpawnBox.CFrame)
								
player:LoadCharacter()

local character = player.Character or player.CharacterAdded:Wait()

if character then
	clonedTrain.Front.Drive:Sit(character:WaitForChild("Humanoid"))
    character:WaitForChild("Humanoid").JumpPower = 0
					
	spawnTrain:FireClient(player)
else
	print("The player " .. player.Name .. ", has no character.")
					
	clonedTrain:Destroy()
end	

Can someone please shed some light on this, because I have seen no one talk about it.

Edit: It’s almost midnight where I live, so please drop replies so I can read them in the morning. :slight_smile:

1 Like

i think when you clone something with PrimaryPart you have to redefine the primary part

I feel like it has something to to with the character wait variable.

Since the if statement doesn’t detect the player initially on spawn, it just deletes the clonedtrain.

But how do you do that if it destroys it?

True, but the train isn’t being destroyed, it’s just the part.

what part of the train is being destroyed?

Literally just the primary part, which is called “MiddlePart”, doubt that will help though.

if it is supposed to destroy the entire train then you could do : for i, v in pairs(ClonedTrain:GetChildren) do
v:Destroy()
end

What difference does that make?

it ensures that it loops trough everything and if a part gives an error that could be it

couldn’t you just call destroy on the model itself?

But nothing would error, because all it’s doing is looping. And plus that code never actually runs, the only thing that is there for is if a player leaves or respawns while the train is spawning

then that script does not delete the train

I’ve never actually seen it delete the train no, because usually that specific scenario never occurs and I would also know because there is a print statement

A bit confused on what you are trying to accomplish here.

If you want to train to only appear client sided, then you would want the model to be cloned by the client, not by the server.

On the client, you would call the clone and spawn it only for the player.

It is being cloned by the server though, it’s a server script

what is the error that the console prints

but its being called multiple times whenever a new player joins in?

The error is in a different script, it says “PrimaryPart is not a valid member of Model”

I think it would be better if I posted the whole script for context…