Index field ? A nil value

I do not understand what I’m doing wrong.
This is my error:
Workspace.Earthquake:19: attempt to index field ‘?’ (a nil value)
if Parts[x].Parent == nil then This is the piece of code it errored me for
I looked for solutions, but none of them seemed to help.

local shakeVal = script.shakeVal
shkMagnitude = 200-script.ShakeMagnitude.Value -- change up here if value increase; change the literal value too
function GetParts(h)
	local g = h:GetChildren()
	for z = 1, #g do
		if g[z]:IsA("Folder") or g[z]:IsA("Configuration") or g[z]:IsA("Model") then
			GetParts(g[z])
		elseif g[z]:IsA("MeshPart") or g[z]:IsA("UnionOperation") or g[z]:IsA("SpawnLocation") or g[z]:IsA("WedgePart") or g[z]:IsA("CornerWedgePart") or g[z]:IsA("Part") then
			table.insert(Parts,z,g[z])
		end
	end
end
GetParts(workspace.EarthquakeArea)
print("nice")
wait(1)
repeat
for x = 1, #Parts do
	if Parts[x].Parent == nil then
		warn("Parts: "..Parts[x]:GetFullName().." Is already destroyed by something!")
	else
		print(Parts[x]:GetFullName())
		local copy = game.ReplicatedStorage.TouchDamage:Clone()
		copy.Parent = Parts[x]
		Parts[x].Anchored = false
		Parts[x]:BreakJoints()
		Parts[x].RotVelocity = Vector3.new(0,shakeVal.Value,0) -- change shakeVal for rotation speed (default 5)
		wait()
		Parts[x].RotVelocity = Vector3.new(0,0,0)
1 Like

Try if not Parts[x].Parent then

I got the same error at the same time.

Basically, this error means that Parts[x] is nil.
A quick fix for if you can’t find why it’s nil would be to replace if Parts[x].Parent == nil then with if Parts[x] and not Parts[x].Parent then. Essentially what this does is check if Parts[x] exists before checking if it has a parent or not.

I think you’re trying to return the full name on line 19, but as it has no parent it has no location in the Explorer.

You’re correct, for the most part.
What happened once I’ve implemented this, code that started with Parts[x] errored, same error.

Parts[x].Anchored = false
Parts[x]:BreakJoints()

And more. I found this out by removing each individual line of code, and all of the Parts[x]. lines were errored.

My apologies, I didn’t notice that it indexed Parts[x] in the else statement too. In this case, you should wrap the entire for loop in an if statement checking if Parts[x] exists, like this:

for x = 1, #Parts do
	if Parts[x] then
		if Parts[x].Parent == nil then
			warn("Parts: "..Parts[x]:GetFullName().." Is already destroyed by something!")
		else
			print(Parts[x]:GetFullName())
			local copy = game.ReplicatedStorage.TouchDamage:Clone()
			copy.Parent = Parts[x]
			Parts[x].Anchored = false
			Parts[x]:BreakJoints()
			Parts[x].RotVelocity = Vector3.new(0,shakeVal.Value,0) -- change shakeVal for rotation speed (default 5)
			wait()
			Parts[x].RotVelocity = Vector3.new(0,0,0)
			-- Whatever code was cut off in the snippet given in the original post
		end
	end
end
1 Like

Thank you so much! You saved me a lot of brain hurting, thanks!

1 Like