Attempt to index nil with "Attachment0" while trying to apply a lineforce to every part of a model

Basically, I’m copying LineForces and Attachments, then parenting them to each child of a model. However, for some reason, while trying to apply Att0C (Attachment0Clone) to LineForce.Attachment0, it gives me the error “Attempt to index nil with ‘Attachment0’” on line 22.

Code

local CHILDREN = Ohio:GetChildren("Part")

local Line = Instance.new("LineForce")
local Attachment0 = Instance.new("Attachment")
local Attachment1 = Instance.new("Attachment")

Attachment0.Name = "Attachment0"
Attachment1.Name = "Attachment1"

for i, v in pairs(CHILDREN) do
	if v.Name == "eSwatini" then
		LineClone = Line:Clone()
		LineClone.Parent = v
		Att0C = Attachment0:Clone()
		Att0C.Parent = v
	end
	if v.Name == "Arkansas" then
		Att1C = Attachment1:Clone()
		Att1C.Parent = v
	end
	LineClone.Attachment0 = Att0C
	LineClone.Attachment1 = Att1C
end

Possible solutions

  1. Make sure that Attachment0 and Attachment1 are properly cloned and parented first before attempting to set the LineForce’s attachment properties.

  2. Check the scope of your variables to make sure that the variables Attachment0 and Attachment1 are accessible from LineClone.

  3. Check the names of the attachments to make sure that they are correctly set to “Attachment0” and “Attachment1” before attempting to assign them to the LineForce.

  4. Check if the LineForce is properly cloned and parented before attempting to assign the attachments to it.

Hit me up if it doesn’t work.

1 Like

All of that can be seen inside the code, so that musn’t be the issue.

1 Like

As the object v cannot have 2 different names the same, the variable LineClone is nil, I don’t really know what you want to try to do, but you can save the variables

local CHILDREN = Ohio:GetChildren()

local Line = Instance.new("LineForce")
local Attachment0 = Instance.new("Attachment")
local Attachment1 = Instance.new("Attachment")

Attachment0.Name = "Attachment0"
Attachment1.Name = "Attachment1"

local LineClone, Att0C, Att1C = nil
for i, v in pairs(CHILDREN) do
	if v.Name == "eSwatini" then
		LineClone = Line:Clone()
		LineClone.Parent = v
		Att0C = Attachment0:Clone()
		Att0C.Parent = v
	end
	if v.Name == "Arkansas" then
		Att1C = Attachment1:Clone()
		Att1C.Parent = v
	end
	if LineClone then
		LineClone.Attachment0 = Att0C
		LineClone.Attachment1 = Att1C
	end
end
1 Like

You haven’t parented your attachments to begin with. Att0c is nil because it’s trying to clone something that doesn’t exist yet.

1 Like

v can have more than one name… It cycles through the children of the model, Ohio (best name), and there are objects of the same type with different names.

1 Like

Yes, but in the same loop no, they are different objects, but if one of the objects is not called eSwatini, it will not create LineClone, so the error occurs.

1 Like

They exist, just because they aren’t parented before I clone them, doesn’t mean they don’t exist. Also, Att0C is parented, in line 16 I parent it. Also, even if this was the case, the script isn’t complaining about Attachment1, so that can’t be true.

1 Like

In which case, can you print both Attachment0 & 1 please.

1 Like

v is the current object the for loop is on in its cycle. Thus, it can have different names because I’m cycling through the children of an object. What you’re saying makes no sense, because I can cycle through every object in workspace and print the name, and they’re going to be different.

1 Like

What exactly would I be printing? It’s existence?

1 Like

Yes. When you first enter the iterative loop:

for i, v...
    print(x, y)

When calling :Clone(), you clone the properties of the object you’re cloning.

The same way as:

foo = nil
bar = foo
print(foo, bar)

… will still print nil both times.

1 Like

The output was

Attachment0 Attachment1
Then the error happened

1 Like

With the Parent property as nil?

1 Like

Yes, I had never parented it after doing Instance.new()

1 Like

Maybe this works

local CHILDREN = Ohio:GetChildren("Part")

local Line = Instance.new("LineForce")
local Attachment0 = Instance.new("Attachment")
local Attachment1 = Instance.new("Attachment")

Attachment0.Name = "Attachment0"
Attachment1.Name = "Attachment1"

for i, v in pairs(CHILDREN) do
	local LineClone, Att0C, Att1C
	if v.Name == "eSwatini" then
		LineClone = Line:Clone()
		LineClone.Parent = v
		Att0C = Attachment0:Clone()
		Att0C.Parent = v
	end
	if v.Name == "Arkansas" then
		Att1C = Attachment1:Clone()
		Att1C.Parent = v
	end
	if LineClone then
		LineClone.Attachment0 = Att0C
		if Att1C then
			LineClone.Attachment1 = Att1C
		end
	end
end

The error message you’re seeing suggests that the Attachment0 property of the LineForce instance is nil , meaning it hasn’t been assigned a value. In your code, you’re trying to assign a value to Attachment0 in the line LineClone.Attachment0 = Att0C . However, this assignment is happening inside a for loop that only applies to certain children of the Ohio object, and it looks like it’s possible for the LineClone object to not be created and assigned a value in this loop.

If that happens, then when the code attempts to set the Attachment0 property on the LineClone object, it will fail because LineClone is nil . To fix this, you can move the assignment of Attachment0 and Attachment1 to the LineClone object outside of the for loop, after you’ve checked that the LineClone object was successfully created and assigned a value.

Here’s how your code might look with this change applied:

Copy code

local CHILDREN = Ohio:GetChildren("Part")

local Line = Instance.new("LineForce")
local Attachment0 = Instance.new("Attachment")
local Attachment1 = Instance.new("Attachment")

Attachment0.Name = "Attachment0"
Attachment1.Name = "Attachment1"

for i, v in pairs(CHILDREN) do
	if v.Name == "eSwatini" then
		LineClone = Line:Clone()
		LineClone.Parent = v
		Att0C = Attachment0:Clone()
		Att0C.Parent = v
	end
	if v.Name == "Arkansas" then
		Att1C = Attachment1:Clone()
		Att1C.Parent = v
	end
end

LineClone.Attachment0 = Att0C
LineClone.Attachment1 = Att1C

I have no idea why, but the for loop randomly breaks after a full iteration. However, this became 2/3 of an iteration when I added a third object, "Tajikistan" the Tajikistans are supposed to be attracted to eSwatinis, and eSwatinis are supposed to be attracted to Arkansas. Using ReactionForceEnabled I can make eSwatinis and Arkansas attracted to each other, which I want, but only two of each are attracted to each other. The Tajikistans are not attracted at all.

Also yes, if you couldn’t tell, I am naming everything after territories because I don’t know what else to name them.

Actually, it is just Roblox being goofy, ignore this.