Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Index Field ? a nil value

There are only 2 possible parents, 'Model' and 'Earthquake Area' My error: Workspace.Earthquake:19: attempt to index field ‘?’ (a nil value) This is the small piece that is giving me the error:

if Parts[x].Parent == nil

I looked for solutions, but none helped me.

Parts = {}
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].Parent.Name)
		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)

2 comments

0

There are 2 things which do not sit well with me. First: The line given in your error output is different from the code you provided. Second: I do not see where "Parts" is defined.

0

Parts is defined at the top. Also, I put 2 scripts into this post on accident. This is the script I'm having trouble with.

1 answer

I replicated the script, and I used it 5 times with different arguments for GetParts()

Replicated Script:

local tab = {}
local function GetParts(h)
	local g = h:GetChildren()
	for i=1, #g do
		warn(i,g[i])
		if g[i]:IsA"Part" then
			table.insert(tab,i,g[i])
		elseif g[i]:IsA"Folder" then
			Parts(g[i])
		end
	end
end
GetParts(workspace)
wait(1)
print("ok")
for x = 1,#tab do
	warn(x,tab[x])
	if tab[x] == nil then
	end
end

Here are the Arguments, I used for GetParts()

GetParts(workspace)
GetParts(workspace.Model)
GetParts(workspace.Folder)
GetParts(workspace.Configuration)
GetParts(workspace.Baseplate)

If the argument is workspace, Folder, or Configuration. Then you will get errors because your table will contain "nil". I believe the reason is because you are inserting objects into a table in a specified table number position. Which causes 3 problems:

Problem(1): You get nil for the number position that should contain an object that is not a part but this only happens if the argument of GetParts() is workspace, Folder, or Configuration

Problem(2): Since you are inserting objects into the table in a specified table number position that is the same as the table number position in the "GetParts() Argument's :GetChildren() Table" then when your GetParts Function runs and it find a Folder, Configuration, or Model, it will run GetParts() on that said, Folder, Configuration, or Model, and it will insert objects into your table on the given specified table number position, therefore it replaces whatever object that is already in that table number position.

Problem(3): Problem(2) will cause Problem(1)

This discussion is preserved as it was. New answers, comments, and votes are closed.