Attempt to index nil with 'position'

  1. What do you want to achieve? I am making a egg hatching system
  2. What is the issue?
uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if player.Character ~= nil and isHatching == false then
			local nearestEgg
			local plrPos = player.Character.HumanoidRootPart.Position
			
			for i, v in pairs(Eggs:GetChildren()) do
				if nearestEgg == nil then
					nearestEgg = v
				else
					if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
						nearestEgg = v
					end
				end
			end

This is my code the error says: Players.az2166.PlayerGui.EggSystem.ClientScript.HatchClient:134: attempt to index nil with ‘Position’

Check that both PrimaryPart’s are valid

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if player.Character ~= nil and isHatching == false then
			local nearestEgg
			local plrPos = player.Character.HumanoidRootPart.Position

			for i, v in pairs(Eggs:GetChildren()) do
				if nearestEgg == nil then
					nearestEgg = v
				elseif v.PrimaryPart and nearestEgg.PrimaryPart then
					if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
						nearestEgg = v
					end
				end
			end
1 Like

they are valid and it still doesnt work think its because of nearestEgg == nil then
nearestEgg = v so i did print (nearestEgg) and it showed up as nil

Hello there!
I see that this is an script from egg hatching tutorial, everything in this script that you showed is alright, I think that the problem is not in the script It’s with something else!

what do you think the problem is? because the primary part is set to eggMesh

You followed the entire egg system tutorial or some parts of it? (just asking)

entire but not finished part 7 atm

Can you add me to team create I hope I can help you, because im not sure what happened there. (it’s alright if not)

can you help in a discord call instead?

No I sadly can’t i need to see the entire scripts models etc. (I have all the scripts done in the tutorial so if you want them contact me in dms) I will still try to find a solution.

alright mate discord dms? or here

Try this

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if player.Character ~= nil and isHatching == false then
			local nearestEgg
			local plrPos = player.Character.HumanoidRootPart
			
			for i, v in pairs(Eggs:GetChildren()) do
				if nearestEgg == nil then
					nearestEgg = v
				else
					if (plrPos.Position - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos.Position).Magnitude then
						nearestEgg = v
					end
				end
			end

I think it errors because you’re indexing a property.

i fixed it by deleting every other egg and keeping the one im using just for now and now when i go to hatch and press E on the egg it says ServerScriptService.Egg_System.Main_Server:5: attempt to index number with number

and when i press on the error it shows me that it has problem w this part of script

			if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
				canHatch = true
			else
				canHatch = false
			end

			if canHatch == true then
				local result = replicatedStorage:WaitForChild("EggHatchingRemotes"):WaitForChild("HatchServer"):InvokeServer(nearestEgg)
				if result ~= nil then
					print("hatch")
					hatchOne(result, nearestEgg)
					end
				else
					print("Cannot Hatch")
				end
			end
		end
	end)

it has problem on the local result = replicatedStorage:WaitForChild(“EggHatchingRemotes”):WaitForChild(“HatchServer”):InvokeServer(nearestEgg)

Can you give more info and send the error?