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)
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.
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