How would I concatenate a number with a string to get the name of an instance?

Here is my code

for i = 1,5,1 do	
		for j, part in pairs(lightning.bolt1:GetDescendants()) do
			if part.Name == "Part" and part:IsA("BasePart") then
				part.Transparency = 0
			elseif part:IsA("PointLight") then
				part.Enabled = true
			end
		end
end

What I am looking to do is replace where it says “bolt1” with bolt + i, so that it goes from bolt1 to bolt5, however I am not sure how this would be concatenated. Maybe there is a better way of doing this?

1 Like

You can use the tostring() function to convert a number to a string and then concatenate it.

1 Like

For what you need, I think you can change

lightning.bolt1

To

lightning["bolt"..i]
--Or if it doesn't work
lightning["bolt"..tostring(i)]

Although this can error if it doesn’t find something with that name, so if bolt1 through 5 doesn’t exist, an error is going to happen

4 Likes

That worked perfectly, thank you so much!

1 Like

??? Why would the memory size have anything to do with it? Try concatenating a one byte string (“a”) with an integer and it’ll still work fine.

1 Like

I wasn’t trying to refer it to that but ok