Hello,

I am working on a train system and when the train has multiple cars I want each car to rename multiple based on which order they are in. When the train gets coupled I’m wanting it to update the number of the added cars.

Each car automatically gets placed into a model of the train its part of

Thanks!

1 Like

Why first row is 1,1,1 and not 1,2,3 ? thats intended?

How are you creating the train, I mean is a model to read? its a table? from where the data to rename comes from?

Try this.

```
objectparent = nil -- put parent containing instances here
for i,v in pairs(objectparent:GetChildren()) do
v.Name = tostring(i)
end
```

1 Like

Nah, when I tried this one before it just names it in random order.

Thank you though

Hmmm thats strange. This is an array so it should BE in ORDER.

Try the code I have supplied, it should work in order.

Ok, so 1, 1, 1 is before each car is renamed and 1, 2, 3 is the result I am wanting after its renamed.

I think its because each part is all named Part so it doesn’t know exactly where in line it should go?

Ok, I think I get it.

Player is able to add more car models into train model at any time, and you want to check in order how those are in the model?

I firstly would go with, setting a value in table or in car model, everytime a new one got added, in that way would be easy to check the order in which were added, and then iterate the model children taking in count its value inside the car…

But, why dont you rename them when those are added in the train?

Yes, that is what I am trying to do

A value of what?

I am trying to rename them in order when they are added but if there were 2 coupled trains already and I wanted to couple the 2 trains together to create 1 train and rename them to be in order again because then it would be like 1, 2, 1, 2 instead of 1, 2, 3, 4.

Thanks

Ok, I’ve given it a try but however when I add more objects whether the object is at the front of the rest of the objects or the rear it’ll be a bit randomized.

Thanks.

The most easiest way I would do it is, when player run the function that adds a new car into the train. Have a IntValue in the model, referencing the current amount of cars, everytime a car is added that value increments by 1, so, I get the train, its value is 0 cars, I add a new car, and increment the value by 1, now train has a value of 1, then I rename the new car with that updated value of 1, next car I add, same, increment current train value by 1, would be 2, I take the current train value to rename this car Im attaching to 2, same for next cars

I already had a value for another feature I could use.

What would I do if I added 2 or more cars at a time would I increase it by 2?

What would I do if there was 4 cars and I uncoupled from the middle creating 2 separate trains?

I think it totally depends on your function that is adding the cars.

One way could be, that the function only attach 1 car at a time, if player wants to attach 2 cars, then the function should run twice.

Its hard to know a quick fix if I dont see your code…

whats a “car”, “cars” for you on your function? and why your function is not able to not know how many cars is adding at the same time, whats a “car model” for the function and how it clones it/attach it, what the car model is made from?..

Many details that could be helpful.

Tables, taking in count the models/references, positions on the train and rescaling the system and tables to include more features, tables

I don’t think there’s anything in my code that could be useful to help understand its mainly just attaching constrains together and whatnot

1 of these is a car

When the train couples it’ll put them into a model.

How about this, the reason for renaming the cars to be in order is to get the very front and the very rear of the train for the purpose of applying the headlights of the very front of the train and applying the taillights of the opposite end from the front so the rear so would there be a way to get the very front and the very rear of the train?

Here is the code I currently use to get the front and rear end:

Here is the attributes:

This value is supposed to be the order the car is in

Yeah, you are in the right track, attributes, values or tables in a script, any of them are for getting the same result. If you track the cars by using attribute when those are added into the train then you have it solved

How would I update each cars value to be depended on the order its in?

Another option I’m thinking is I could loop through each model and if both couplers in that model are coupled then I know its not the front or rear but if none or 1 is coupled then its either the front or rear?

I don’t think I need to get the exact order for every car I’m pretty sure I just need to get the front and rear uncoupled sides for lighting and only having the touched even active for the uncoupled sides purposes.

This was the solution. The purpose of me needing to find the order the each car is in was to have the trains headlights on at the front and the trains taillights on at the back which was based gets car 1 and then gets the last car if your on the north side of the train or if your on the south side it would get the end car first and then then the front car and setup the lights from there but now it looks through each car and if both couplers are coupled then it knows that its not the front or rear car but if 1 coupler is coupled and the other coupler is not then it knows its either the front or rear.

Edit:

This wouldn’t work or would be harder to do for regrouping each car when cars uncouple but I’ve found a more efficient way and an actual way to rename them in order of their position for if anyone else is needing something similar.

```
local Train = workspace.Train:GetChildren()
table.sort(Train, function(A, B)
return A.Position.X < B.Position.X
end)
for Index, Car in Train do
Car.Name = Index
end
```

Thanks to those who helped.

1 Like