How to get from Name1 Name2?

Hi,

I have 2 parts called “Part1” and “Part” is there a way to change the name of “Part” to “Part2” and then to “Part3”. It’s a string so i don’t know if i can use maths in it. If anyone know if it possible please post how i can achieve this! Thanks.

1 Like

Yes. Using a for loop like this:

for i = 1,5 do
local part = Instance.new('Part')
part.Name = "Part"..i
part.Parent = path.to.parent

But there are other ways to name it.

1 Like

You can add the code in your script that names the part. I’m guessing you’re asking how to do it automatically? I don’t think you can though so. As @Quwanterz this is the only way.

Edit: I would shorten it though if you’re gonna use it a lot.

1 Like

My bad, it’s not the only way.

1 Like

Only way I know. (30 charactersssssss)

1 Like

You can’t really shorten it more than that if you’re using a numeric loop.

Since a numeric approach has already been proposed (Quwanterz accidentally left out an end), I’ll use a repeat loop.

local i = 0
desiredParts = 10

repeat
      i += 1
      local p = Instance.new("Part")
      p.Name = "Part"..i
      p.Parent = workspace
until i == desiredParts

a numeric for would be the best way to do this though.

You can perform actions on numbers that are strings, but not all strings containing numbers.

print(math.log10("100") ) --> 2
2 Likes

I mean’t as in function not shortening.

1 Like

This is completely useless way. It’s too complicated – if you want to keep things simple, use for loops.

1 Like

I don’t think you took the time to fully read my response,

I stated this is just another approach to the problem, since the numeric for was already proposed (didn’t want to offer parrot responses) not to mention, it’s not really complicated at all- it’s as simple as a numeric loop in function.

1 Like