What to do about "Attempt to concatenate nil with string" error?

Ok, so I have been working on a script that changes a ton of names of things inside of models, but it seems to have stopped working.

I get a strange error on this script, which goes through and renames things inside of a model, but I get an error: “attempt to concatenate nil with string”

This is the script. (This is copy + pasted into the command bar when used.)

local bigGroup = game.Workspace.Tycoons.T_RedTeam.TRT_Grid.TRTG_Line01ToLine10.TRTG_Line03
local groups = bigGroup:GetChildren()
local renameSplit = string.split(bigGroup.Name, "e")
local rename = "TRTGL"
local split
local group


for int, value in pairs(groups) do
	group = value:GetChildren()
	for i, v in pairs(group) do
		split = string.split(string.split(v.Name, "_")[2], "quare")
		v.Script.Name = rename .. renameSplit[2] .. split[1] .. split[2] .. "_Script"
	end
end

and this is the line that gives the error:

v.Script.Name = rename .. renameSplit[2] .. split[1] .. split[2] .. "_Script"

Anyone know what’s going on?

EDIT: I looked it up and this is what concatenate means: “Link (things) together in a chain or series.”

2 Likes

Well if it doesn’t work then one of the strings you’re trying to concatenate doesn’t exist (aka nil) so maybe try printing each one of them and see which one is nil, then fix it.

1 Like

String concatenation would be combining two string variables to make one string

x='Hello'
y='world'
z= x..' '..y

When your getting the error ‘concatenate nil with string’, it means one of those variables is a nil value.
It might be renameSplit[2] thats the problem (nil value), based on the order of errors.

Either way, something you can learn from this error is building more robust code. You might run into a name which is formatted differently then you expect, you should check if all the values you expect are set, and if not make a continue statement to avoid error for that object.

1 Like

I used the printing thing, but that didn’t help too much. Apparently I messed up renaming certain objects, so they were not named how I expected. Whoops.

2 Likes