Attempt to index nil with 'Position'

Local Script(not full):

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

So I have a script to open the eggs pets, but in the part where I try to understand the distance, I get an error that I do wrong?

2 Likes

Post full code. Post full error.

check the primaryparts of both nearestEgg and v

PrimaryPart can be nil check that

alr

full script:
Local:

local rp = game:GetService("ReplicatedStorage")
local runSerivce = game:GetService("RunService")
local tweenService = game:GetService("TweenService")
local uis = game:GetService("UserInputService")

local player = game:GetService("Players").LocalPlayer

local things = rp:WaitForChild("EggThings")
local eggs = workspace:WaitForChild("Eggs")
local Module3D = require(rp:WaitForChild("Module3D"))

local maxDisplayDistance = 15
local canHatch = false
local isHatching = false

wait(0.5)

local function animateBillBoard(billboard, openOrClose)
	if openOrClose == true then
		wait(0.4)
		tweenService:Create(billboard, TweenInfo.new(.4), {Size = UDim2.new(9, 0, 9, 0)}):Play()
	else
		tweenService:Create(billboard, TweenInfo.new(.4), {Size = UDim2.new(0, 0, 0, 0)}):Play()
		wait(0.5)
		billboard.Enabled = false
	end
	wait(0.5)
end

for i, v in	 pairs(eggs:GetChildren()) do
	local eggThings = things:FindFirstChild(v.Name)
	
	if eggThings ~= nil then
		
		local billboardTemp = script.Template:Clone()
		local container = billboardTemp:WaitForChild("Container")
		local container1 = container.Container1
		local display = container1:WaitForChild("Display")
		
		billboardTemp.Parent = script.Parent.Parent.EggBillboards
		billboardTemp.Name = v.Name
		billboardTemp.Adornee = v.Egg_1
		billboardTemp.Enabled = true
		
		local things = {}
		
		for x, thing in pairs(eggThings:GetChildren()) do
			table.insert(things, thing.Rarity.Value)
		end
		
		table.sort(things)
		
		for _, rarity in pairs(things) do
			for _, thing in pairs(eggThings:GetChildren()) do
				if thing.Rarity.Value == rarity then 
					wait(0.1)

					if thing.Rarity.Value == 50 then
						local rarity = thing.Rarity.Value


						local clonedTemp1 = container1:Clone()

						clonedTemp1.Name = thing.Name
						clonedTemp1.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
						clonedTemp1.Visible = true
						clonedTemp1.Parent = container

						local thingModel = Module3D:Attach3D(clonedTemp1.Display,thing:Clone())
						thingModel:SetDepthMultiplier(1.2)
						thingModel.Camera.FieldOfView = 5 
						thingModel.Visible = true

						runSerivce.RenderStepped:Connect(function()
							thingModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
						end)

					end

					if thing.Rarity.Value == 35 then
						local clonedTemp2 = container.Container2:Clone()

						clonedTemp2.Name = thing.Name
						clonedTemp2.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
						clonedTemp2.Visible = true
						clonedTemp2.Parent = container

						local thingModel = Module3D:Attach3D(clonedTemp2.Display,thing:Clone())
						thingModel:SetDepthMultiplier(1.2)
						thingModel.Camera.FieldOfView = 5 
						thingModel.Visible = true

						runSerivce.RenderStepped:Connect(function()
							thingModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
						end)

					end

					if thing.Rarity.Value == 14 then
						local clonedTemp3 = container.Container3:Clone()

						clonedTemp3.Name = thing.Name
						clonedTemp3.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
						clonedTemp3.Visible = true
						clonedTemp3.Parent = container

						local thingModel = Module3D:Attach3D(clonedTemp3.Display,thing:Clone())
						thingModel:SetDepthMultiplier(1.2)
						thingModel.Camera.FieldOfView = 5 
						thingModel.Visible = true

						runSerivce.RenderStepped:Connect(function()
							thingModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
						end)

					end 

					if thing.Rarity.Value == 0.96 then
						local clonedTemp4 = container.Container4:Clone()

						clonedTemp4.Name = thing.Name
						clonedTemp4.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
						clonedTemp4.Visible = true
						clonedTemp4.Parent = container

						local thingModel = Module3D:Attach3D(clonedTemp4.Display,thing:Clone())
						thingModel:SetDepthMultiplier(1.2)
						thingModel.Camera.FieldOfView = 5 
						thingModel.Visible = true

						runSerivce.RenderStepped:Connect(function()
							thingModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
						end)

					end

					wait(0.5)

					--if thing.Rarity.Value == 0.04 then
					--	local clonedTemp5 = container.Container5:Clone()

					--	clonedTemp5.Name = thing.Name
					--	clonedTemp5.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
					--	clonedTemp5.Visible = true
					--	clonedTemp5.Parent = container

					--	print(clonedTemp5, thing.Rarity.Value)
					--end
					break
				else
					continue
				end
			end  
		end
		
		runSerivce.RenderStepped:Connect(function()
			if player:DistanceFromCharacter(v.Core.Position) < maxDisplayDistance then
				canHatch = true
				billboardTemp.Enabled = true
				animateBillBoard(billboardTemp, true)
			else
				animateBillBoard(billboardTemp, false)
				canHatch = false
			end
		end)
	end
end


uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if canHatch == true and 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
			
			local result = rp:WaitForChild("HatchServer"):InvokeServer(nearestEgg)
			
			if result == true then
				print("hatching")
				isHatching = true
				wait(3) 
				isHatching = false
			else
				print("cannot hatch")
			end
		else
			print("Can't hatch")
		end
	end
end)

Script:

--local players = game:GetService("Players")

--local function playerAdded(player)
--	for i, v in pairs(player:WaitForChild("leaderstats"):GetChildren()) do
--		v:Clone().Parent = player
--	end
--end


game.ReplicatedStorage.HatchServer.OnServerInvoke = function(player, egg)
	local eggModel = workspace.Eggs:FindFirstChild(egg.Name)
	
	if eggModel ~= nil then
		local price = eggModel.Price.Value
		local currency = eggModel.Currency.Value
		
		if player.leaderstats[currency].Value >= price then
			return true
		else
			return false
		end
	end
end

error:

PrimaryPart:

Try:

if eggModel.PrimaryPart then

Edit
Sorry, I didn’t read the error properly. You could try replacing:

if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then

with:

if v.PrimaryPart and (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then

This should check to see if the PrimaryPart exists. If it doesn’t then it will not run that code.

If that doesn’t work, try printing out v in your for loop on line 180 and debug from there.

1 Like

you should check your models and if they have a primarypart…

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