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

Attempt to index nil with find first child ?

  15:31:08.179  Players.Nimbzee.Backpack.Lightsaber.Functional.Server:505: attempt to index nil with 'FindFirstChild'  -  Server - Server:505

hello, I'm a beginner scripter that is still learning to code. I am receiving this error on line 505 but don't know why.

I have a data store that saves crystals. in the player's folder, there will be many different bool values saved. i.e green, one I have in my folder of saved data.

these were a mix taken from the toolbox, some help from others and myself.

there is a module script that has the colors, and the keys. you press the key on your keyboard and it changes the color.

module.colors = {

	blue   = {Outer = color(0, 136, 255); Inner = color(117, 212, 255); Key = Enum.KeyCode.B};
	cyan   = {Outer = color(52, 255, 242); Inner = color(170, 255, 255); Key = Enum.KeyCode.C};
	green  = {Outer = color(0, 170, 0); Inner = color(123, 255, 165); Key = Enum.KeyCode.G};
	orange = {Outer = color(255, 85, 0 ); Inner = color(255, 180, 105); Key = Enum.KeyCode.T};
	purple = {Outer = color(54, 12, 150); Inner = color(167, 128, 199); Key = Enum.KeyCode.P};
	red    = {Outer = color(198, 0, 3); Inner = color(255, 171, 171); Key = Enum.KeyCode.R};
	white  = {Outer = color(211, 211, 211); Inner = color(148, 200, 255); Key = Enum.KeyCode.Z};
	jade = {Outer = color(63, 127, 94); Inner = color(255, 230, 105); Key = Enum.KeyCode.J};
	teal = {Outer = color(0, 114, 127); Inner = color(255, 230, 105); Key = Enum.KeyCode.N};
	silver = {Outer = color(122,122,122); Inner = color(255, 230, 105); Key = Enum.KeyCode.V};
	pink = {Outer = color(255, 255,  0); Inner = color(255, 142, 246); Key = Enum.KeyCode.L};
	yellow = {Outer = color(255, 255,  0); Inner = color(255, 230, 105); Key = Enum.KeyCode.Y};
	
}

there is a server script to check when they press one of these and change it like so. this is where the error is.

Recolor = function(data,plr)
		local TargetBlade
		if Dual then
			TargetBlade = {
				[1] = ActiveSabers[1].Blade,
			}
			if ActiveSabers[2] then
				TargetBlade[2] = ActiveSabers[2].Blade
			end
		else
			TargetBlade = Blades
		end
		for _, Blade in next, TargetBlade do

			local Target = "Emitter"
			if Blade.Name == "Blade2" then
				Target = "Emitter2"
			end
			Blade.FX.Glow.Color = data.Outer
			for _, v in next, Blade[Target]:GetChildren() do            
				if data[v.Name] then
					if plr:FindFirstChild("Crystals"):FindFirstChild("Green") then
						v.Color = ColorSequence.new(data[v.Name])
					end
				else
					v.Color = ColorSequence.new(data.Outer, data.Inner)
				end
			end

		end
	end,

at the line where it says if plr:find first child seems to be where i get my error. im also confused on another question. how can i check the color in the folder without having to type all the colors manually? i am a beginner so any help would be appriciated. thanks alot!

3 comments

0

Confirm that plr is being passed as a valid value to the function?

0

I made an edit to my answer.

0

Make sure you are passing down plr in "Recolor" function.

1 answer

The "Parent:FindFirstChild('name')" function returns the child of a parent IF IT EXISTS. If the child doesn't exist, the ":FindFirstChild()" function will return nil (nil means no value). My guess is that when you put this code down below:

if plr:FindFirstChild("Crystals"):FindFirstChild("Green") then

The "plr:FindFirstChild('Crystals')" most likely returned nil (keep in mind that if it does return nil, it doesn't notify the script writer in the output). A simplified way how the code is acting right now is:

nil:FindFirstChild('Crystals')

This code is unacceptable to the computer. Most likely, the "Crystals" object utilized in the ":FindFirstChild()" object doesn't exist. I don't know why, but you didn't post enough of your code or info so I can't help you with that. A way that you can write this code that can help debug (fix) the problem is:

local crystals = plr:FindFirstChild('Crystals')
if crystals then
	print('Crystals obj exists.')
	local green = crystals:FindFirstChild('Green')
	if green then
		print('Green obj exists.')
	end
end

If both of them print, then you have found a solution to your problem.

Also, if you want to loop through many colors, you want to use an in pairs loop. Here's sample code down below:

local myTable = {'Red', 'Green', 'Blue'}

for i, v in pairs (myTable) do
	print(v .. ' ')
end

The amount of times the in pairs loop loops the table is variable and depends on how many items are in the table. (E.G. If 2 items are in the table, the in pairs loop loops 2 times. If 3 items are in the table, the in pairs loop loops 3 times).

The i, v are two variables that represent the items in the table (i, v can be changed to any name that you want they are variables, just make sure to put the variables in the same order as i, v if you intend to use them the same way as these predefined variables).

"i" represents the index of the items in the table. An example of using "i" is shown below:

local myTable = {'Red', 'Green', 'Blue'}

for i, v in pairs (myTable) do
	print(myTable[i] .. ' ')
end

"v" represents the value of the items in the table. An example of using "v" is shown below:

local myTable = {'Red', 'Green', 'Blue'}

for i, v in pairs (myTable) do
	print(v .. ' ')
end

One last thing. If you want to get all of the children from a parent object, you can use the ":GetChildren()" method.

local myObject = workspace.Object
local myObjectTable = myObject:GetChildren() -- Returns a table of the object's children.

for i, v in pairs(myObjectTable) do
	print(v .. " ")
end

One way that you use the ":GetChildren()" method to stay organized with your current situation is to make a folder that contains a bunch of objects with certain color values. You can use the ":GetChildren()" to find the certain object and get it's color. Here's an example of a linear search that you might perform:

(The name of the colors are the same name as the objects in this code).

local Folder = workspace.ColorFolder
local colorTable = Folder:GetChildren()

function get_color(colorName)
	for i, v in pairs(colorTable) do
		if colorTable[i].Name == colorName then
			return colorTable[i].Color3 -- Return the Color3 value of the color searched.
		end
	end
	return nil -- Couldn't find color
end

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