Attempt to index nil with 'HeadColor3'

I am currently working on something, and I have this problem.

So I am making an avatar editor and I want to save it to the DataStore.
Here is the code from the Avatar Editor

Main.Save.MouseButton1Click:Connect(function()
	local bodyColorSetup = {}
	local shirtSetup
	local pantsSetup
	local graphicSetup
	
	for index,color in Main.Noob:GetChildren() do
		bodyColorSetup[color.Name] = color.BackgroundColor3:ToHex()
	end
	shirtSetup = Main.Shirt.Text
	pantsSetup = Main.Pants.Text
	graphicSetup = Main.Graphic.Text
	
	print("Awaiting request...")
	local PreparedData = {
		["BodyColors"] = bodyColorSetup;
		["Accessories"] = {0};
		["CharacterMesh"] = "Roblox2.0";
		["Clothing"] = {
			["Shirt"] = shirtSetup;
			["Pants"] = pantsSetup;
			["ShirtGraphic"] = graphicSetup
		}
	}

	local EncodedData
	local success, err = pcall(function()
		EncodedData = HttpService:JSONEncode(PreparedData)
	end)

	if not success then
		warn(`Error occured sending data: {err}`)
		return
	end
	print(EncodedData)
	game.ReplicatedStorage.Avatar.SaveAvatar:FireServer(EncodedData)
	warn("Complete")
end)

Now, upon sending the event the encoded data, it’ll show the table but this error

I am genuinely confused… here’s how the event works

game.ReplicatedStorage.Avatar.SaveAvatar.OnServerEvent:Connect(function(plr, data: array)
	if not data then return end
	
	print(data)
	local savedData = JSON:JSONEncode({
		["BodyColors"] = {
			--Color3.fromRGB(69, 0, 245); --head color
			--Color3.fromRGB(245, 205, 48); --left arm color
			--Color3.fromRGB(75, 151, 75); --left leg color
			--Color3.fromRGB(245, 205, 48); --right arm color
			--Color3.fromRGB(75, 151, 75); --right leg color
			--Color3.fromRGB(13, 105, 172); --torso color

			["HeadColor3"] = data.BodyColors.HeadColor3; --head color
			["LeftArmColor3"] = data.BodyColors.LeftArmColor3; --left arm color
			["LeftLegColor3"] = data.BodyColors.LeftLegColor3; --left leg color
			["RightArmColor3"] = data.BodyColors.RightArmColor3; --right arm color
			["RightLegColor3"] = data.BodyColors.RightLegColor3; --right leg color
			["TorsoColor3"] = data.BodyColors.TorsoColor3; --torso color
		};
		["Accessories"] = data.Accessories;
		["CharacterMesh"] = data.CharacterMesh;
		["Clothing"] = {
			["Shirt"] = data.Clothing.Shirt;
			["Pants"] = data.Clothing.Pants;
			["ShirtGraphic"] = data.Clothing.ShirtGraphic
		}
	})

	local success,err = pcall(function()
		return AvatarData:SetAsync(plr.UserId, savedData)
	end)
	if not success then
		warn(`Error while rendering {plr.Name} avatar: {err}`)
		return
	end
	warn(plr.Name.." Avatar has been saved!")
end)

Would be kind enough for some help, thanks!
Edit: If you are wondering, data: array used to exist but I removed it during finding the problem, lol. But I added it back.

When using string keys in a dictionary, you need to index them using the bracket notation:

local myDictionary = {
     ["keyOne"] = "valueOne",
     ["keyTwo"] = "valueTwo",
}

print(myDictionary["keyOne"])

Alternatively, when you construct a dictionary without using the bracket notation, you could do essentially what you did in your code and access the relevant key like an attribute or property:

local myOtherDictionary = {
     keyOne = "valueOne",
     keyTwo = "valueTwo",
}

print(myOtherDictionary.keyOne)

So instead of data.BodyColors you would have data["BodyColors"]. This is just a quirk of Lua.

It seems like you explained something I used while making this.

It still returns that error.

 ServerScriptService.RenderAvatar:24: attempt to index nil with 'HeadColor3'  -  Server - RenderAvatar:24
  19:09:13.802  Stack Begin  -  Studio
  19:09:13.802  Script 'ServerScriptService.RenderAvatar', Line 24  -  Studio - RenderAvatar:24
  19:09:13.802  Stack End  -  Studio

Ah I see, my bad!

I think the issue here is that you are sending JSON-encoded data to the server (i.e. a string):

local success, err = pcall(function()
	EncodedData = HttpService:JSONEncode(PreparedData)
end)

if not success then
	warn(`Error occured sending data: {err}`)
	return
end
print(EncodedData)
game.ReplicatedStorage.Avatar.SaveAvatar:FireServer(EncodedData)

which the server attempts to interpret as a dictionary:

game.ReplicatedStorage.Avatar.SaveAvatar.OnServerEvent:Connect(function(plr, data: array)
...

Maybe try just sending this data without the conversion to a string? Something like

game.ReplicatedStorage.Avatar.SaveAvatar:FireServer(PreparedData)

In the future, if you are sending data through a RemoteEvent or other similar networking, make sure you are familiar with the limitations on the types of these parameters since this data is erased without warning.

I think this is a little more helpful/relevant :stuck_out_tongue:

1 Like

MY GOD, YOU ARE RIGHT.

Thank you soooooo much for this, it works now!! :DDD
I’ll probably learn from this next time, maybe.
image

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.