Attempt to index nil with 'Clone'

  1. What do you want to achieve? I want my pet system to work

  2. What is the issue? Even though the pet’s name gets printed and the egg is not nil, the error comes

  3. What solutions have you tried so far? I checked devhub. I also debugged my code.

This may seem like another post i made but this error is completely different

-- client

local rep = game:GetService("ReplicatedStorage")
local runs = game:GetService("RunService")
local model3D = require(rep:WaitForChild("Module3D"))
local tweeny = game:GetService("TweenService")
local uis = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local cam = workspace.Camera
local pets = rep:WaitForChild("Pets")
local eggs = workspace:WaitForChild("Nidus")
local distance  = 30

local canhatch = nil
local ishatch = false
local hatch1connect = nil
local cooldown = false
wait(3)

local function animatebilly(billboard , openorclose)
	if openorclose == true then
		tweeny:Create(billboard, TweenInfo.new(0.1),{Size = UDim2.new(15, 0,20, 0)}):Play()
	else
		tweeny:Create(billboard, TweenInfo.new(0.2),{Size = UDim2.new(0,0,0, 0)}):Play()
		wait(.3)
		billboard.Enabled = false
	end
	wait(1)
end

local function togglegui(bool)
	for i,v in pairs(script.Parent.Parent.Parent:GetChildren()) do
		if v.Name ~= "EggSystem" then
			v.Enabled = bool
		end
	end
end



for i,v in pairs(eggs:GetChildren()) do
	local eggpets = pets:FindFirstChild(v.Name)

	if eggpets ~= nil then


		local billboardtemp = script.Template:Clone()
		local container = billboardtemp:WaitForChild("Cointainer")
		local frame = container:WaitForChild("Main_Frame")
		local template = frame:WaitForChild("Template")
		local billboardegg = script.Parent.Parent.Eggboards
		local port = template:WaitForChild("Port")

		billboardtemp.Adornee = v.EggMesh
		billboardtemp.Name = v.Name
		billboardtemp.Enabled = true
		billboardtemp.Parent = billboardegg

		local petz = {}

		for x, pet in pairs(eggpets:GetChildren()) do
			table.insert(petz, pet.Rarity.Value)

		end
		table.sort(petz)

		for i = 1, math.floor(#petz/2) do
			local j = #petz - i + 1
			petz[i], petz[j] = petz[j], petz[i]
		end

		for _,rarity in pairs(petz) do
			for _, pet in pairs(eggpets:GetChildren()) do
				if pet.Rarity.Value == rarity then
					local rarity = pet.Rarity.Value



					local clonedtemp = template:Clone()
					clonedtemp.Name = pet.Name

					clonedtemp.Rarity.Text = tostring(pet.Rarity.Value).."%"
					clonedtemp.Visible = true
					clonedtemp.Parent = frame

					local petmodel = model3D:Attach3D(clonedtemp.Port,pet:Clone())
					petmodel:SetDepthMultiplier(1.2)
					petmodel.Camera.FieldOfView = 5
					petmodel.Visible = true

					runs.RenderStepped:Connect(function()
						petmodel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(10),0,0))
					end)
					break
				else
					continue
				end
			end
		end



		runs.RenderStepped:Connect(function()
			if plr:DistanceFromCharacter(v.EggMesh.PrimaryPart.Position) < distance then
				if billboardtemp.Name ~= v.Name then
					canhatch = true

					animatebilly(billboardtemp, true)
					billboardtemp.Enabled = true
				else
					canhatch = true

					animatebilly(billboardtemp, true)
					billboardtemp.Enabled = true

				end
			else
				animatebilly(billboardtemp, false)

				canhatch = false
			end

		end)
	end
end

local function hatch1(petName,egg)
	togglegui(false)
	
	egg = nil 
	petName = nil

	local plrps = plr.Character.HumanoidRootPart.Position

	for i,v in pairs(eggs:GetChildren()) do
		for i,v in pairs(eggs:GetChildren()) do
			if egg == nil then
				egg = v
			else
				if (plrps - v.EggMesh.PrimaryPart.Position).Magnitude < (egg:WaitForChild("EggMesh",true).PrimaryPart.Position).Magnitude then
					egg = v
				end
			end
		end
	end

	for i,v in pairs(pets[egg.Name]:GetChildren()) do
		if petName == nil then
			petName = v
		end
	end
	
	--where error appears

	local pet = pets[egg.Name]:FindFirstChild(petName):Clone()

	
	print(pet.Name)
	local eggmesh = egg:FindFirstChild("EggMesh"):Clone()
	ishatch = true

	script.Parent.Parent.PetDusp.petnaem.Text = tostring(petName)
	
	for i,v in pairs(eggmesh:GetChildren()) do
		if v:isA("BasePart") then
			v.Anchored = true
			v.CanCollide = false
		end
	end
	hatch1connect = runs.RenderStepped:Connect(function()
		local cf = CFrame.new(0, 0,-eggmesh.PrimaryPart.Size.Z * 2) * CFrame.Angles(math.sin(time() *4)*2,math.sin(time() *4) *2,math.sin(time() *4) * 2)
		eggmesh:SetPrimaryPartCFrame(cam.CFrame * cf)
	end)
	eggmesh.Parent = cam
	wait(3)
	for i,v in pairs(eggmesh:GetChildren()) do
		if v:isA("BasePart") then
			tweeny:Create(v, TweenInfo.new(0.3),{Transparency = 1}):Play()
		end
	end
	wait(0.3)
	hatch1connect:Disconnect()
	eggmesh:Destroy()
	script.Parent.Parent.PetDusp.Visible = true

	local petmodel = model3D:Attach3D(script.Parent.Parent.PetDusp,pet)
	petmodel:SetDepthMultiplier(1.2)
	petmodel.Camera.FieldOfView = 5
	petmodel.Visible = true

	runs.RenderStepped:Connect(function()
		petmodel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
	end)
	wait(3)
	tweeny:Create(script.Parent.Parent.PetDusp:FindFirstChildOfClass("ViewportFrame"), TweenInfo.new(0.5), {ImageTransparency = 1}):Play()
	wait(0.5)
	for i, v in pairs(script.Parent.Parent.PetDusp:GetDescendants()) do
		if v:IsA("ViewportFrame") then
			v:Destroy()
			script.Parent.Parent.PetDusp.Visible = true
		end
	end
	script.Parent.Parent.PetDusp.Visible = false

	togglegui(true)
	_G.newTemp(petName)
end

local function triplehatch(petName,petName2,petName3,egg)
	togglegui(false)
	egg = nil
	petName = nil
	petName2 = nil
	petName3 = nil
	
	local plrps = plr.Character.HumanoidRootPart.Position
	
	for i,v in pairs(eggs:GetChildren()) do
		for i,v in pairs(eggs:GetChildren()) do
			if egg == nil then
				egg = v
			else
				if (plrps - v.EggMesh.PrimaryPart.Position).Magnitude < (egg:WaitForChild("EggMesh",true).PrimaryPart.Position).Magnitude then
					egg = v
				end
			end
		end
	end
	
	for i,v in pairs(pets[egg.Name]:GetChildren()) do
		if petName == nil then
			petName = v
		end
	end
	
	for i,v in pairs(pets[egg.Name]:GetChildren()) do
		if petName2 == nil then
			petName2 = v
		end
	end
	
	
	for i,v in pairs(pets[egg.Name]:GetChildren()) do
		if petName3 == nil then
			petName3 = v
		end
	end

	--where error occurs

	local pet = pets[egg.Name]:FindFirstChild(petName):Clone()
	local pet2 = pets[egg.Name]:FindFirstChild(petName2):Clone()
	local pet3 = pets[egg.Name]:FindFirstChild(petName3):Clone()


	script.Parent.Parent.PetDusp.petnaem.Text = tostring(petName)
	script.Parent.Parent.PetDusp2.petnaem.Text = tostring(petName2)
	script.Parent.Parent.PetDusp3.petnaem.Text = tostring(petName3)
	ishatch = true
	local eggmesh = egg:FindFirstChild("EggMesh"):Clone()
	local eggmesh2 = egg:FindFirstChild("EggMesh"):Clone()
	local eggmesh3 = egg:FindFirstChild("EggMesh"):Clone()
	for i,v in pairs(eggmesh:GetChildren()) do
		if v:isA("BasePart") then
			v.Anchored = true
			v.CanCollide = false
		end
	end

	for i,v in pairs(eggmesh2:GetChildren()) do
		if v:isA("BasePart") then
			v.Anchored = true
			v.CanCollide = false
		end
	end

	for i,v in pairs(eggmesh3:GetChildren()) do
		if v:isA("BasePart") then
			v.Anchored = true
			v.CanCollide = false
		end
	end


	hatch1connect = runs.RenderStepped:Connect(function()
		local cf = CFrame.new(0, 0,-eggmesh.PrimaryPart.Size.Z * 2) * CFrame.Angles(math.sin(time() *4)*2,math.sin(time() *4) *2,math.sin(time() *4) * 2)
		eggmesh:SetPrimaryPartCFrame(cam.CFrame * cf)

		local cf2 = CFrame.new(6, 0,-eggmesh.PrimaryPart.Size.Z * 2) * CFrame.Angles(math.sin(time() *4)*2,math.sin(time() *4) *2,math.sin(time() *4) * 2)
		eggmesh2:SetPrimaryPartCFrame(cam.CFrame * cf2)

		local cf3 = CFrame.new(-6, 0,-eggmesh.PrimaryPart.Size.Z * 2) * CFrame.Angles(math.sin(time() *4)*2,math.sin(time() *4) *2,math.sin(time() *4) * 2)
		eggmesh3:SetPrimaryPartCFrame(cam.CFrame * cf3)
	end)
	eggmesh.Parent = cam
	eggmesh2.Parent = cam
	eggmesh3.Parent = cam
	wait(3)
	ishatch = false
	for i,v in pairs(eggmesh:GetChildren()) do
		if v:isA("BasePart") then
			tweeny:Create(v, TweenInfo.new(0.3),{Transparency = 1}):Play()
		end
	end

	for i,v in pairs(eggmesh2:GetChildren()) do
		if v:isA("BasePart") then
			tweeny:Create(v, TweenInfo.new(0.3),{Transparency = 1}):Play()
		end
	end

	for i,v in pairs(eggmesh3:GetChildren()) do
		if v:isA("BasePart") then
			tweeny:Create(v, TweenInfo.new(0.3),{Transparency = 1}):Play()
		end
	end
	wait(0.3)
	hatch1connect:Disconnect()
	eggmesh:Destroy()
	eggmesh2:Destroy()
	eggmesh3:Destroy()
	script.Parent.Parent.PetDusp.Visible = true
	script.Parent.Parent.PetDusp2.Visible = true
	script.Parent.Parent.PetDusp3.Visible = true

	local petmodel = model3D:Attach3D(script.Parent.Parent.PetDusp,pet)
	petmodel:SetDepthMultiplier(1.2)
	petmodel.Camera.FieldOfView = 5
	petmodel.Visible = true

	local petmodel2 = model3D:Attach3D(script.Parent.Parent.PetDusp2,pet2)
	petmodel2:SetDepthMultiplier(1.2)
	petmodel2.Camera.FieldOfView = 5
	petmodel2.Visible = true

	local petmodel3 = model3D:Attach3D(script.Parent.Parent.PetDusp3,pet3)
	petmodel3:SetDepthMultiplier(1.2)
	petmodel3.Camera.FieldOfView = 5
	petmodel3.Visible = true


	runs.RenderStepped:Connect(function()
		petmodel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
		petmodel2:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
		petmodel3:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
	end)


	wait(3)
	tweeny:Create(script.Parent.Parent.PetDusp:FindFirstChildOfClass("ViewportFrame"), TweenInfo.new(0.5), {ImageTransparency = 1}):Play()
	tweeny:Create(script.Parent.Parent.PetDusp2:FindFirstChildOfClass("ViewportFrame"), TweenInfo.new(0.5), {ImageTransparency = 1}):Play()
	tweeny:Create(script.Parent.Parent.PetDusp3:FindFirstChildOfClass("ViewportFrame"), TweenInfo.new(0.5), {ImageTransparency = 1}):Play()

	wait(0.5)
	for i, v in pairs(script.Parent.Parent.PetDusp:GetDescendants()) do
		if v:IsA("ViewportFrame") then
			v:Destroy()
			script.Parent.Parent.PetDusp.Visible = true
		end
	end

	for i, v in pairs(script.Parent.Parent.PetDusp2:GetDescendants()) do
		if v:IsA("ViewportFrame") then
			v:Destroy()
			script.Parent.Parent.PetDusp2.Visible = true
		end
	end

	for i, v in pairs(script.Parent.Parent.PetDusp3:GetDescendants()) do
		if v:IsA("ViewportFrame") then
			v:Destroy()
			script.Parent.Parent.PetDusp3.Visible = true
		end
	end
	script.Parent.Parent.PetDusp.Visible = false
	script.Parent.Parent.PetDusp2.Visible = false
	script.Parent.Parent.PetDusp3.Visible = false
	ishatch = false
	togglegui(true)
	_G.newTemp(petName)
	_G.newTemp(petName2)
	_G.newTemp(petName3)
end


uis.InputBegan:Connect(function(input)
	local billboardtemp = script.Template
	local clonedtemp = billboardtemp.Cointainer.button
	if input.KeyCode == Enum.KeyCode.E then
		if plr.Character ~= nil and ishatch == false then
			local plrps = plr.Character.HumanoidRootPart.Position
			local nearsetg = nil
			for i,v in pairs(eggs:GetChildren()) do
				for i,v in pairs(eggs:GetChildren()) do
					if nearsetg == nil then
						nearsetg = v
					else
						if (plrps - v.EggMesh.PrimaryPart.Position).Magnitude < (nearsetg:WaitForChild("EggMesh",true).PrimaryPart.Position).Magnitude then
							nearsetg = v
						end
					end
				end
			end
			if plr:DistanceFromCharacter(nearsetg.EggMesh.PrimaryPart.Position) < distance then
				canhatch = true

			else
				canhatch = false
			end
			if canhatch == true then
				if canhatch == true then
					local result = rep: WaitForChild("EggHatchingRemotes"):WaitForChild("hatchserver"):InvokeServer(nearsetg)
					if result ~= nil then
						if not cooldown then
							cooldown = true
							hatch1(result,nearsetg)
							wait(0.1)
							cooldown = false
						end
					end

				end
			end
		end

	end

	if input.KeyCode == Enum.KeyCode.R  then
		if plr.Character ~= nil and ishatch == false then
			local plrps = plr.Character.HumanoidRootPart.Position
 			local nearsetg = nil
			for i,v in pairs(eggs:GetChildren()) do
				for i,v in pairs(eggs:GetChildren()) do
					if nearsetg == nil then
						nearsetg = v
					else
						if (plrps - v.EggMesh.PrimaryPart.Position).Magnitude < (nearsetg:FindFirstChild("EggMesh",true).PrimaryPart.Position).Magnitude then
							nearsetg = v
						end
					end
				end
			end
			if plr:DistanceFromCharacter(nearsetg.EggMesh.PrimaryPart.Position) < distance then
				canhatch = true

			else
				canhatch = false
			end
			if canhatch == true then
				if canhatch == true then
					local result1 , result2, result3 = rep.EggHatchingRemotes.hatch3pets:InvokeServer(nearsetg)
					if result1 ~= nil and result2 ~= nil and result3 ~= nil then
						if not cooldown then
							cooldown = true
							triplehatch(result1, result2, result3, nearsetg)
							wait(0.1)
							cooldown = false
						end


					end

				end
			end
		end

	end
end)

for i,v in pairs(script.Parent.Parent.Eggboards:GetChildren()) do

	local ebtn = v.Cointainer.button.Open_one
	local rbtn = v.Cointainer.button.Open_three

	ebtn.MouseButton1Click:Connect(function()
		if plr.Character ~= nil and ishatch == false then

			local nearsetg = nil
			local plrps = plr.Character.HumanoidRootPart.Position

			for i,v in pairs(eggs:GetChildren()) do
				for i,v in pairs(eggs:GetChildren()) do
					if nearsetg == nil then
						nearsetg = v
					else
						if (plrps - v.EggMesh.PrimaryPart.Position).Magnitude < (nearsetg:FindFirstChild("EggMesh",true).PrimaryPart.Position).Magnitude then
							nearsetg = v
						end
					end
				end
				if plr:DistanceFromCharacter(nearsetg.EggMesh.PrimaryPart.Position) < distance then
					canhatch = true

				else
					canhatch = false
				end
				if canhatch == true then

					local result = rep:WaitForChild("EggHatchingRemotes"):WaitForChild("hatchserver"):InvokeServer(nearsetg)
					print(result)
					if result ~= "Cannot hatch" then
						if not cooldown then
							cooldown = true
							hatch1(result,nearsetg)
							wait(0.1)
							cooldown = false
						end


					end

				end
			end
		end

	end)

	rbtn.MouseButton1Click:Connect(function()
		if plr.Character ~= nil and ishatch == false then
			local plrps = plr.Character.HumanoidRootPart.Position
			local nearsetg = nil
			for i,v in pairs(eggs:GetChildren()) do
				for i,v in pairs(eggs:GetChildren()) do
					if nearsetg == nil then
						nearsetg = v
					else
						if (plrps - v.EggMesh.PrimaryPart.Position).Magnitude < (nearsetg:FindFirstChild("EggMesh",true).PrimaryPart.Position).Magnitude then
							nearsetg = v
						end
					end
				end
			end
			if plr:DistanceFromCharacter(nearsetg.EggMesh.PrimaryPart.Position) < distance then
				canhatch = true

			else
				canhatch = false
			end
			if canhatch == true then
				if canhatch == true then
					local result1 , result2, result3 = rep.EggHatchingRemotes.hatch3pets:InvokeServer(nearsetg)
					if result1 ~= nil and result2 ~= nil and result3 ~= nil then
							triplehatch(result1, result2, result3, nearsetg)
							wait(0.1)
							cooldown = false
						end


					end

				end
		end 
	end)
end
--server

wait(2)

local plrs = game:GetService("Players")

local function addplrs(player)
	for i,v in pairs(game.ServerStorage.EggHatchingData:GetChildren()) do
		v:Clone().Parent = player
	end
end

plrs.PlayerAdded:Connect(addplrs)

for i, player in pairs(plrs:GetPlayers()) do
	addplrs(player)
end
local function choosepet(egg)
	local Chance = math.random(1,100)
	local Counter = 0
	for _,pet in pairs(game.ReplicatedStorage.Pets[egg.Name]:GetChildren()) do 
		Counter = Counter + pet.Rarity.Value
		if Chance <= Counter then
			return pet.Name 
		end
	end
 end



game.ReplicatedStorage.EggHatchingRemotes.hatchserver.OnServerInvoke = function(player,egg)
	local eggmodel = workspace.Nidus:FindFirstChild(egg.Name)
	if eggmodel ~= nil then
		local price = eggmodel.Price
		local currency = eggmodel.Currency.Value
		
		if player.leaderstats[currency].Value >= price.Value then
			player.leaderstats[currency].Value = player.leaderstats[currency].Value - price.Value
			local chosenpet = choosepet(egg)
			return chosenpet
		else
			return false
		end
	end
end

game.ReplicatedStorage.EggHatchingRemotes.hatch3pets.OnServerInvoke = function(player,egg)
	local eggmodel = workspace.Nidus:FindFirstChild(egg.Name)
	if eggmodel ~= nil then
		local price = eggmodel.Price
		local currency = eggmodel.Currency.Value

		if player.leaderstats[currency].Value >= price.Value * 3 then
			player.leaderstats[currency].Value = player.leaderstats[currency].Value - price.Value * 3
			local chosenpet1 = choosepet(egg)
			local chosenpet2 = choosepet(egg)
			local chosenpet3 = choosepet(egg)
			return chosenpet1,chosenpet2,chosenpet3
		else
			return "Cannot hatch"
		end
	end
end

Can you post which line the error occurs on, preferably both the line number and a copy of the line itself

try
local pet = pets[egg.Name]:FindFirstChild(tostring(petName)):Clone()

if it didn’t work you can try this

local pet = pets[egg.Name]:FindFirstChild(tostring(petName))
local petClone

if pet then
petClone = pet
print("PetClone Is Now "…tostring(pet)
end

2 Likes

i think it’s this line, it seems to be calling a nonexistent attachment

actually there is like 4 of them and he did point it out saying “–here is where the error occurred” or something like that

the error is in line 156… in the Client script

--where error appears
local pet = pets[egg.Name]:FindFirstChild(petName):Clone()

--where error occurs
local pet = pets[egg.Name]:FindFirstChild(petName):Clone()
local pet2 = pets[egg.Name]:FindFirstChild(petName2):Clone()
local pet3 = pets[egg.Name]:FindFirstChild(petName3):Clone()

try printing print(petName) before these happen, based on the loops above you are probably getting an instance or stringValue so use

-- print check debugging, or use your watch list
print(petName)
-- if instance
pets[egg.Name]:FindFirstChild(petName.Name):Clone()
-- if StringValue
pets[egg.Name]:FindFirstChild(petName.Value):Clone()

thank you , it was an instance.