Help with Egg Hatch System Attempt to index nil with EggParts

So when I walk away to the egg and press T, which is auto open, it says Attempt to index nil with EggParts."

Sorry for the laggy video
Video:

Client Script

local EggAnimationHandler = {}

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local MarketplaceService = game:GetService("MarketplaceService")

local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

local GameUI = PlayerGui:WaitForChild("GameUI")
local EggUI = PlayerGui:WaitForChild("EggUI")

local GameAssets = ReplicatedStorage.GameAssets

local Events = GameAssets.Events
local Modules = GameAssets.Modules
local Particles = GameAssets.Particles
local PetModels = GameAssets.Pets
local Sounds = GameAssets.Sounds

local BasicFunctions = require(Modules.BasicFunctions)
local LBConnection = require(Modules.LBConnection)
local ItemsHandler = require(Modules.ItemsHandler)

local UIHandler = require(script.Parent.UIHandler)

local MainFrame = GameUI.MainFrame

local EggViewFrame = GameUI.EggViewFrame

local CanHatch = script.Parent.EggViewHandler.CanHatch
local CurrentTarget = script.Parent.EggViewHandler.CurrentTarget

local function CheckHasPass()
	local hasPass = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 120967282)
	if Player.Gamepasses:FindFirstChild("Fast Hatch") then
		hasPass = hasPass or Player.Gamepasses["Fast Hatch"].Value == true
	end

	if hasPass then
		return true
	else
		return false
	end
end

local function GetHatchSpeed()
	local HatchSpeed = 1
	
	if CheckHasPass() == true then
		HatchSpeed *= 2
	end
	
	if Player.Upgrades.EggHatchSpeed.Value == true then
		HatchSpeed *= 1.25
	end
	
	
	return HatchSpeed
end

local function RotateEgg(EggCFrame, Rot, NumberTime)
	local EggTweenInfo = TweenInfo.new(NumberTime, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local Goal = {Value = CFrame.Angles(0,0, math.rad(Rot))}
	local TweenEgg = TweenService:Create(EggCFrame, EggTweenInfo, Goal)

	TweenEgg:Play()
end

local function EggAnimation(Type, Egg, Result)
	
	if Type == nil and Egg == nil and Result == nil then
		return
	end
	
	if Type == "Single" then
		
		if CanHatch.Value == true then
			CanHatch.Value = false
			
			
			GameUI.Enabled = false
			
			local HatchSpeed = GetHatchSpeed()

			local HatchFrame = script.HatchFrame:Clone()

			local CloneEgg = workspace.Eggs:FindFirstChild(Egg).EggParts:Clone()
			local ClonePet = PetModels:FindFirstChild(Result.PetName):Clone()

			local CurrentCamera = workspace.CurrentCamera

			local Camera = Instance.new("Camera")
			Camera.Parent = HatchFrame
			HatchFrame.CurrentCamera = Camera
			HatchFrame.Parent = EggUI

			local EggTweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Bounce)

			local EggDelay = 0.075
			local DestroyTime = 4

			for _, egg in pairs(CloneEgg:GetChildren()) do
				if egg:IsA("BasePart") then

					egg.Anchored = false
					egg.CanCollide = false
				end
			end

			CloneEgg.Parent = CurrentCamera

			local EggCFrame = Instance.new("CFrameValue")


			local DisplayEgg = RunService.RenderStepped:Connect(function()
				CloneEgg:SetPrimaryPartCFrame(CurrentCamera.CFrame * CFrame.new(0,0, -6) * EggCFrame.Value)
			end)
			
			EggCFrame.Value = CFrame.new(0, 8, 0)
			
			local TweenEgg = TweenService:Create(EggCFrame , EggTweenInfo, {Value = CFrame.new()})

			TweenEgg:Play()

			Sounds.Woosh:Play()

			TweenEgg.Completed:Wait()

			for i = 1, 9, 1 * HatchSpeed do
				Sounds.TurnEgg:Play()
				Sounds.TurnEgg.Looped = true
				RotateEgg(EggCFrame, 20, EggDelay)
				task.wait(EggDelay)
				RotateEgg(EggCFrame, -20, EggDelay)
				task.wait(EggDelay)
				RotateEgg(EggCFrame, 0 , EggDelay)
				EggDelay -= .005
			end

			Sounds.TurnEgg.Looped = false
			Sounds.TurnEgg:Stop()


			RotateEgg(EggCFrame, 0, EggDelay)
			
			local Pos = Vector3.new(0,0,0)
			ClonePet:SetPrimaryPartCFrame(CFrame.new(Pos))

			ClonePet.Parent = CurrentCamera

			task.wait(EggDelay / HatchSpeed)


			for _,v in pairs(CloneEgg:GetChildren()) do
				if v:IsA("BasePart") then
					local FadeEgg = TweenService:Create(v, TweenInfo.new(0.01), {Transparency = 1})

					FadeEgg:Play()

					FadeEgg.Completed:Wait()
				end
			end



			HatchFrame.Pet1.Visible = true
			HatchFrame.Hatched1.Visible = true
			HatchFrame.PetName1.Visible = true
			HatchFrame.PetRarity1.Visible = true

			if ItemsHandler.Pets[Result.PetName].Rarity and Player.AutoDelete[ItemsHandler.Pets[Result.PetName].Rarity].Value then
				HatchFrame.AutoDelete1.Visible = true
			else
				HatchFrame.AutoDelete1.Visible = false
			end

			if Result.PetType == "Shiny" then
				HatchFrame.PetName1.Text = "Shiny " .. Result.PetName
				HatchFrame.PetName1.TextColor3 = Color3.fromRGB(0,191,255)
				HatchFrame.PetName1.UIStroke.Color = Color3.fromRGB(0, 162, 216)

				for _,pet in ipairs(ClonePet:GetDescendants()) do
					if pet:IsA("BasePart") then

						if BasicFunctions.Shade(pet.Color) == true and pet.Material == Enum.Material.Neon then
							pet.Material = "SmoothPlastic"
						end

						pet.Color = BasicFunctions.InvertColor(pet.Color)
					end
				end
			elseif Result.PetType == "Golden" then
				HatchFrame.PetName1.Text = "Golden " .. Result.PetName
				HatchFrame.PetName1.TextColor3 = Color3.fromRGB(255, 215, 0)
				HatchFrame.PetName1.UIStroke.Color = Color3.fromRGB(198, 165, 0)

				for _,pet in ipairs(ClonePet:GetDescendants()) do
					if pet:IsA("BasePart") then

						pet.Color = Color3.fromRGB(255, 215, 0)
					end
				end
			else
				HatchFrame.PetName1.Text = Result.PetName
			end

			HatchFrame.PetRarity1.Text = ItemsHandler.Pets[Result.PetName].Rarity
			HatchFrame.PetRarity1.TextColor3 = ItemsHandler.RarityColor[ItemsHandler.Pets[Result.PetName].Rarity][1]
			HatchFrame.PetRarity1.UIStroke.Color = ItemsHandler.RarityColor[ItemsHandler.Pets[Result.PetName].Rarity][2]



			local DisplayPet = RunService.RenderStepped:Connect(function()
				ClonePet:SetPrimaryPartCFrame(CurrentCamera.CFrame * CFrame.new(0, 0 , -6) * CFrame.new() * CFrame.Angles(0, math.rad(180), 0))
			end)
			
			if ItemsHandler.Pets[Result.PetName].Rarity == "Legendary" or ItemsHandler.Pets[Result.PetName].Rarity == "Secret" then
				local LegendaryParticle = Particles.LegendaryEffect:Clone()
				LegendaryParticle.Parent = ClonePet.PrimaryPart
				LegendaryParticle:Emit(50)
			end
		
			Sounds.Hatched:Play()

			task.wait(DestroyTime / HatchSpeed)

			local FadePet = TweenService:Create(HatchFrame.Pet1.ViewportFrame, TweenInfo.new(0.1), {Transparency = 1})


			FadePet:Play()

			FadePet.Completed:Wait()
			
			DisplayEgg:Disconnect()
			DisplayPet:Disconnect()
			ClonePet:Destroy()
			CloneEgg:Destroy()
			HatchFrame:Destroy()

			GameUI.Enabled = true
			CanHatch.Value = true
		end
	end
	
	if Type == "Triple" then
		

		if CanHatch.Value == true then
			CanHatch.Value = false
			
			GameUI.Enabled = false
			
			local HatchSpeed = GetHatchSpeed()
			
			local HatchFrame = script.HatchFrame:Clone()

			local CurrentCamera = workspace.CurrentCamera

			local Camera = Instance.new("Camera")
			Camera.Parent = HatchFrame
			HatchFrame.CurrentCamera = Camera
			HatchFrame.Parent = EggUI

			Camera.CFrame = CFrame.new(0, 0, 0):ToWorldSpace(CFrame.new(0, 0, 5.5))

			for i = 1, 3 do
				task.spawn(function()
					local CloneEgg = workspace.Eggs:FindFirstChild(Egg).EggParts:Clone()

					local ClonePet = PetModels:FindFirstChild(Result[i].PetName):Clone()

					if CloneEgg ~= nil then
						local EggTweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Bounce)

						local EggDelay = 0.075
						local DestroyTime = 4

						for _, egg in pairs(CloneEgg:GetChildren()) do
							if egg:IsA("BasePart") then

								egg.Anchored = false
								egg.CanCollide = false
							end
						end

						CloneEgg.Parent = CurrentCamera


						local EggPos
						local PetRot

						if i == 1 then
							EggPos = 0
							PetRot = 180
						elseif i == 2 then
							EggPos = -5
							PetRot = 220
						elseif i == 3 then
							EggPos = 5
							PetRot = 140
						end

						local EggCFrame = Instance.new("CFrameValue")

						local DisplayEgg = RunService.RenderStepped:Connect(function()
							CloneEgg:SetPrimaryPartCFrame(CurrentCamera.CFrame * CFrame.new(EggPos, 0, -6) * EggCFrame.Value)
						end)
						
						EggCFrame.Value = CFrame.new(0, 8, 0)


						local TweenEgg = TweenService:Create(EggCFrame , EggTweenInfo, {Value = CFrame.new()})

						TweenEgg:Play()

						Sounds.Woosh:Play()

						TweenEgg.Completed:Wait()


						for i = 1, 9, 1 * HatchSpeed do
							Sounds.TurnEgg:Play()
							Sounds.TurnEgg.Looped = true
							RotateEgg(EggCFrame, 20, EggDelay)
							task.wait(EggDelay)
							RotateEgg(EggCFrame, -20, EggDelay)
							task.wait(EggDelay)
							RotateEgg(EggCFrame, 0 , EggDelay)
							EggDelay -= .005
						end

						Sounds.TurnEgg.Looped = false
						Sounds.TurnEgg:Stop()
						

						RotateEgg(EggCFrame, 0, EggDelay)

						local Pos = Vector3.new(0,0,0)
						ClonePet:SetPrimaryPartCFrame(CFrame.new(Pos))

						ClonePet.Parent = CurrentCamera

						task.wait(EggDelay / HatchSpeed)

						for _,v in pairs(CloneEgg:GetChildren()) do
							if v:IsA("BasePart") then
								local FadeEgg = TweenService:Create(v, TweenInfo.new(0.01), {Transparency = 1})

								FadeEgg:Play()

								FadeEgg.Completed:Wait()
							end
						end

						HatchFrame["Pet" .. i].Visible = true
						HatchFrame["Hatched" .. i].Visible = true
						HatchFrame["PetName" .. i].Visible = true
						HatchFrame["PetRarity" .. i].Visible = true

						if ItemsHandler.Pets[Result[i].PetName].Rarity ~= "Secret" and Player.AutoDelete[ItemsHandler.Pets[Result[i].PetName].Rarity].Value then
							HatchFrame["AutoDelete" .. i].Visible = true
						else
							HatchFrame["AutoDelete" .. i].Visible = false
						end

						if Result[i].PetType == "Shiny" then
							HatchFrame["PetName" .. i].Text = "Shiny " .. Result[i].PetName
							HatchFrame["PetName" .. i].TextColor3 = Color3.fromRGB(0,191,255)
							HatchFrame["PetName" .. i].UIStroke.Color = Color3.fromRGB(0, 162, 216)

							for _,pet in ipairs(ClonePet:GetDescendants()) do
								if pet:IsA("BasePart") then

									if BasicFunctions.Shade(pet.Color) == true and pet.Material == Enum.Material.Neon then
										pet.Material = "SmoothPlastic"
									end

									pet.Color = BasicFunctions.InvertColor(pet.Color)
								end
							end
						elseif Result[i].PetType == "Golden" then
							HatchFrame["PetName" .. i].Text = "Golden " .. Result[i].PetName
							HatchFrame["PetName" .. i].TextColor3 = Color3.fromRGB(255, 215, 0)
							HatchFrame["PetName" .. i].UIStroke.Color = Color3.fromRGB(198, 165, 0)

							for _,pet in ipairs(ClonePet:GetDescendants()) do
								if pet:IsA("BasePart") then

									pet.Color = Color3.fromRGB(255, 215, 0)
								end
							end
						else
							HatchFrame["PetName" .. i].Text = Result[i].PetName
						end

						HatchFrame["PetRarity" .. i].Text = ItemsHandler.Pets[Result[i].PetName].Rarity
						HatchFrame["PetRarity" .. i].TextColor3 = ItemsHandler.RarityColor[ItemsHandler.Pets[Result[i].PetName].Rarity][1]
						HatchFrame["PetRarity" .. i].UIStroke.Color = ItemsHandler.RarityColor[ItemsHandler.Pets[Result[i].PetName].Rarity][2]


						local DisplayPet = RunService.RenderStepped:Connect(function()

							ClonePet:SetPrimaryPartCFrame(CurrentCamera.CFrame * CFrame.new(EggPos,0, -6) * CFrame.new() * CFrame.Angles(0, math.rad(PetRot), 0))
						end)
						
						if ItemsHandler.Pets[Result[i].PetName].Rarity == "Legendary" or ItemsHandler.Pets[Result[i].PetName].Rarity == "Secret" then
							local LegendaryParticle = Particles.LegendaryEffect:Clone()
							LegendaryParticle.Parent = ClonePet.PrimaryPart
							LegendaryParticle:Emit(50)
						end

						Sounds.Hatched:Play()

						task.wait(DestroyTime / HatchSpeed)

						local FadePet = TweenService:Create(HatchFrame["Pet" .. i].ViewportFrame, TweenInfo.new(0.1), {Transparency = 1})


						FadePet:Play()

						FadePet.Completed:Wait()

						DisplayEgg:Disconnect()
						DisplayPet:Disconnect()
						ClonePet:Destroy()
						CloneEgg:Destroy()
						HatchFrame:Destroy()

						CanHatch.Value = true
						GameUI.Enabled = true
					end
				end)
			end
		end
	end
end

local function AutoOpenEgg(Type)
	local Egg = ItemsHandler.Eggs[CurrentTarget.Value]

	if Egg ~= nil then
		if Player.Stats.AutoEggOwned.Value == true then
			while true do

				if CurrentTarget.Value ~= "None" then
					if (Player.Character.HumanoidRootPart.Position - workspace.Eggs:FindFirstChild(CurrentTarget.Value).Anchor.Position).Magnitude <= 10 then
						local Result, Message = Events.OpenEgg:InvokeServer(CurrentTarget.Value, Type)
						if Message == nil and Result ~= "Error" and Result ~= nil then
							if Player.Upgrades.TripleEggOwned.Value == true then
								EggAnimation("Triple", CurrentTarget.Value, Result)
							else
								EggAnimation("Single", CurrentTarget.Value, Result)
							end
						else
							if Message ~= nil then
								UIHandler:OpenFrame("PetNotificationFrame")
								MainFrame.PetNotificationFrame.TextFrame.Warning.Text = tostring(Result .. ", " .. Message)
								break
							end
						end
					end
				else
					break
				end
				task.wait()
			end
		--else
		--	print("Auto Open is set to false.")
		end
	end
end


local function OnOpenEgg(Type)
	local Egg = ItemsHandler.Eggs[CurrentTarget.Value]

	if Type ~= "Auto Open" then
		if Egg ~= nil and CurrentTarget.Value ~= "None" or nil then
			if (Player.Character.HumanoidRootPart.Position - workspace.Eggs:FindFirstChild(CurrentTarget.Value).PrimaryPart.Position).Magnitude <= 10 then
				local Result, Message = Events.OpenEgg:InvokeServer(CurrentTarget.Value, Type)
				if Message == nil and Result ~= "Error" and Result ~= nil then
					EggAnimation(Type, CurrentTarget.Value, Result)
				else
					if Message ~= nil then
						UIHandler:OpenFrame("PetNotificationFrame")
						MainFrame.PetNotificationFrame.TextFrame.Warning.Text = tostring(Result .. ", " .. Message)
					end
				end
			end
		end
	end
end

local function OnPressE()
	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.E then
				if UserInputService:GetFocusedTextBox() == nil and CurrentTarget.Value ~= "None" or nil then
					OnOpenEgg("Single")
				end
			end
		end
	end)
end

local function OnPressR()
	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.R then
				if UserInputService:GetFocusedTextBox() == nil and CurrentTarget.Value ~= "None" or nil then
					OnOpenEgg("Triple")
				end
			end
		end
	end)
end

local function OnPressT()
	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.T then
				if UserInputService:GetFocusedTextBox() == nil and CurrentTarget.Value ~= "None" or nil then
					AutoOpenEgg("Auto")
				end
			end
		end
	end)
end

function EggAnimationHandler:Int()
	OnPressE()
	OnPressR()
	OnPressT()

	EggViewFrame.Single.MouseButton1Down:Connect(function()
		if CurrentTarget.Value ~= "None" or nil then
			OnOpenEgg("Single")
		end
	end)

	EggViewFrame.Triple.MouseButton1Down:Connect(function()
		if CurrentTarget.Value ~= "None" or nil then
			OnOpenEgg("Triple")
		end
	end)

	EggViewFrame.Auto.MouseButton1Down:Connect(function()
		if CurrentTarget.Value ~= "None" or nil then
			AutoOpenEgg("Auto")
		end
	end)

	Events.OpenEgg.OnClientInvoke = EggAnimation
end


return EggAnimationHandler

It can be difficult to look through a script with so many lines, like this one. So, if you can add comments to the line(s) that the error is occurring at, that may be helpful.

I see this line in your code twice. Is this the line that errors?

local CloneEgg = workspace.Eggs:FindFirstChild(Egg).EggParts:Clone()

Is Egg an Instance or a string? Because if Egg is an Instance, the script thinks you’re searching for an Instance in workspace.Eggs that’s named Egg’s ClassName, not Egg’s Name. And workspace.Eggs:FindFirstChild(Egg) will return nil if there’s nothing in workspace.Eggs named Egg’s ClassName – if Egg was a Model, the script would be searching for a child in workspace.Eggs named “Model”.

1 Like

well the Egg is a string to get the egg model in the workspace.

OK. Is the line I provided the one that the error occurs at? And have you printed Egg to make sure that the string is a name of an “egg”?

Yeah it does print when you are near the egg and hatch but when I walk away too fast it causes the egg system to break.

1 Like