Attempt to index nil with 'clone'

I am following a tutorial on how to make a pets system, getting this error message. I tried rearranging the ‘petclone.Parent = studio’ to right after where it says ‘petClone = pet:Clone()’.

I’m new to scripting so bare with me here.

1 Like

For future reference you should post your code wrapped in 6 backticks (3 in front, 3 behind) to make it easier for people to review and analyze your code.

As for your error, it’s pretty self explanatory. You’re trying to clone something that doesn’t exist.

Can you post the entire code so we can see where you’re getting pet from?

local camera = game.Workspace.Camera
local studio = game.Workspace.Studio
local TweenService = game:GetService("TweenService")
game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function(pet)
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = studio.CamPart.CFrame
	
	wait(1)
	
	for i = 1, 50, 1 do
		studio.EggInStudio.Size = studio.EggInStudio.Size + Vector3.new(0.1,0.1,0.1)
		wait(0.01)
	end
	
	local explosion = Instance.new("Explosion")
	explosion.BlastRadius = 15
	explosion.BlastPressure = 0
	explosion.Position = studio.EggInStudio.Position
	explosion.ExplosionType = Enum.ExplosionType.NoCraters
	explosion.DestroyJointRadiusPercent = 0
	explosion.Parent = studio.EggInStudio
	studio.EggInStudio.Transparency = 1
	
	local petClone = pet:Clone()
	
	for i, v in pairs(petClone:GetChildren()) do
		if v:IsA("BasePart") then
			v.Anchored = true
		end
	end
	
	for i, v in pairs(studio.Confetti:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = true
		end
	end
	
	petClone:SetPrimaryPartCFrame(CFrame.new(studio.EggInStudio.Position,studio.CamPart.Position))
	petClone.Parent = studio
	
	local tweenInfo = TweenInfo.new(2,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out,0,false,0)
	local tween = TweenService:Create(camera, tweenInfo, {CFrame = CFrame.new(petClone.PrimaryPart.Position + (petClone.PrimaryPart.CFrame.lookVector * 5) + Vector3.new(0,0.75,0),petClone.PrimaryPart.Position)})
	tween:Play()
	
	wait(5)
	
	for i, v in pairs(studio.Confetti:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = false
		end
	end
	
	camera.CameraType = Enum.CameraType.Custom
	studio.EggInStudio.Transparency = 0
	studio.EggInStudio.Size = Vector3.new(13.421, 17.184, 13.402)
	
	
	
end)

Above is the local script.

local petModule = {}
petModule.pets = {
	
	["Legendary"] = {
		game.ReplicatedStorage.Pets["Demonic Dominus"];
		game.ReplicatedStorage.Pets["Shadow Dominus"];
	};
	["Epic"] = {
		game.ReplicatedStorage.Pets.Virus;
		game.ReplicatedStorage.Pets.Alien;
		game.ReplicatedStorage.Pets.Infection;
	};
	["Uncommon"] = {
		game.ReplicatedStorage.Pets["Plasma Overlord"];
		game.ReplicatedStorage.Pets["Pumpkin Fellow"];
		game.ReplicatedStorage.Pets["Mad Scientist"];
		game.ReplicatedStorage.Pets.Cowboy;
	};
	["Common"] = {
		game.ReplicatedStorage.Pets.Bee;
		game.ReplicatedStorage.Pets.Fedora;
	};
	
	
	
	
	
}

petModule.rarities = {
	
	["Legendary"] = 7;
	["Epic"] = 13;
	["Uncommon"] = 25;
	["Common"] = 55;
	
}

petModule.chooseRandomPet = function()
	local randomNumber = math.random(1,100)
	local counter = 0
	for rarity, weight in pairs(petModule.rarities) do
		counter = counter + weight
		if randomNumber <= counter then 
			local rarityTable = petModule.pets[rarity]
			local chosenPet = rarityTable[math.random(1,#rarityTable)]
			
			return chosenPet
		end
	end
end

return petModule


Above is pets & rarity data.

local camera = game.Workspace.Camera
local studio = game.Workspace.Studio
local TweenService = game:GetService("TweenService")
game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function(pet)
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = studio.CamPart.CFrame
	
	wait(1)
	
	for i = 1, 50, 1 do
		studio.EggInStudio.Size = studio.EggInStudio.Size + Vector3.new(0.1,0.1,0.1)
		wait(0.01)
	end
	
	local explosion = Instance.new("Explosion")
	explosion.BlastRadius = 15
	explosion.BlastPressure = 0
	explosion.Position = studio.EggInStudio.Position
	explosion.ExplosionType = Enum.ExplosionType.NoCraters
	explosion.DestroyJointRadiusPercent = 0
	explosion.Parent = studio.EggInStudio
	studio.EggInStudio.Transparency = 1
	
	local petClone = pet:Clone()
	
	for i, v in pairs(petClone:GetChildren()) do
		if v:IsA("BasePart") then
			v.Anchored = true
		end
	end
	
	for i, v in pairs(studio.Confetti:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = true
		end
	end
	
	petClone:SetPrimaryPartCFrame(CFrame.new(studio.EggInStudio.Position,studio.CamPart.Position))
	petClone.Parent = studio
	
	local tweenInfo = TweenInfo.new(2,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out,0,false,0)
	local tween = TweenService:Create(camera, tweenInfo, {CFrame = CFrame.new(petClone.PrimaryPart.Position + (petClone.PrimaryPart.CFrame.lookVector * 5) + Vector3.new(0,0.75,0),petClone.PrimaryPart.Position)})
	tween:Play()
	
	wait(5)
	
	for i, v in pairs(studio.Confetti:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = false
		end
	end
	
	camera.CameraType = Enum.CameraType.Custom
	studio.EggInStudio.Transparency = 0
	studio.EggInStudio.Size = Vector3.new(13.421, 17.184, 13.402)
	
	
	
end)


And lastly above is the script that is displayed on the post,

yeah it wanted to record something of a nil value, or as you said ‘something that doesn’t exist’.

Do you have any solutions, though?

You’re trying to clone pet, but you never defined what pet is within the local script. You have to set pet to be equal to something before you try and clone it. Based on your structure you need to add the following:

At the top of your script

local petModule = require(Put the location of pets and rarity data here)

Right before you try to clone the pet add this:

local pet = petModule.choseRandomPet()
1 Like

New error, and doesn’t work.

This could mean 3 things:

  1. Your module script named “PetModule” doesn’t exist (most likely not the case because if you’ve been watching the AlvinBlox tutorial and are this far then the PetModule script should have already been created)
  2. Your module script is in the wrong place
  3. You spelled “PetModule” wrong

image

I believe the script is in the right place, PetModule is spelled correctly and PetModule exsists.

:skull:

try :FindFirstChild(), you aren’t waiting for it to load, it exists.

Still, doesn’t work. I forgot to mention this but, half of the animation does actually work, but the camera, confetti and pet don’t show up.

Is the pet showing up anywhere else in studio? Go into server form and see if the pet clones anywhere else besides the studio area.

Could you show us the script that fires the game.ReplicatedStorage.HatchEgg.OnClientEvent?


image

Here it is. Scroll up to see it. @EmeraldLimes

Found the issue, why are you doing require()?

I meant the server script that fires the client, which should be something along the lines of this:

game.ReplicatedStorage.HatchEgg:FireClient(plr,...)

You’re passing the argument pet from the server to the client when you do FireClient and OnClientEvent, meaning, if you passed nil as pet from the server to the client, then you would get the following error, Attempt to index nil with clone

I was suggested it here. hmmmm

I removed it, still doesn’t work. :neutral_face:

I’ve got to go for the night… I’ll check the replies in the morning. Any information helps!

You are trying to access PetModule from the client, and the client cannot view ServerScriptService as it can only be accessed by the server. Move the PetModule Module to ReplicatedStorage and make sure to change the require line to the new location.

1 Like