Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I dont understand why im getting a nil value with clone?

I don't understand at all why I'm getting a nil value with "Clone"?

Please someone help me I don't understand one bit why this is happening

INFO: I have a folder in ReplicatedStorage called "Pets", All of my pets go inside there

INFO: I have a remote event inside ReplicatedStorage as well called HatchEgg than i have leaderstats connect to this all.

I have a Module script inside ServerScriptService than i have a model with a ClickDetector that has a Script (Not a localscript) inside that model than on top of that i have a localscript inside StarterGui

##Error code:

03:27:43.179  Players.Chumbis20.PlayerGui.LocalScript:28: attempt to index nil with 'Clone'  -  Client - LocalScript:28

##Script #1 [PetModule]:

local petModule = {}

petModule.pets = {
	
	["Legendary"] = {
		game.ReplicatedStorage.Pets.Bat;
		game.ReplicatedStorage.Pets.Bunny;
		game.ReplicatedStorage.Pets.Mouse;
	};
	
	["Rare"] = {
		game.ReplicatedStorage.Pets.FireDeer;
		game.ReplicatedStorage.Pets.FireFox;
		game.ReplicatedStorage.Pets.FirePiggy;
		game.ReplicatedStorage.Pets.LavaBear;
		game.ReplicatedStorage.Pets.MagmaDoggy;
	};
	
	["Uncommon"] = {
		game.ReplicatedStorage.Pets.Dino;
		game.ReplicatedStorage.Pets.EmeraldGolem;
	};
	
	["Common"] = {
		game.ReplicatedStorage.Pets.Golem;
	};	
}

-- Weighted Selection
-- 100 Total weight
petModule.rarities = {
	
	["Legendary"] = 5; -- 5% Chance
	
	["Rare"] = 15; -- 15% Chance
	
	["Uncommon"] = 30; -- 30% Chance
	
	["Common"] = 50; -- 50% Chance
}

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

##Script #2 [Script inside the model]:

local Cost = 1000
local PetModule = require(game.ServerScriptService:WaitForChild("PetModule"))

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	
	if player.leaderstats.Cash.Value >= Cost then
		
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - Cost
		
		local pet = PetModule.chooseRandomPet()
		
		print(pet.Name.." selected")
		
		game.ReplicatedStorage.HatchEgg:FireClient(player)
		
	end
	
end)

##Script #3 [local Script inside StarterGui]:

This is the script giving me issues

local TweenService = game:GetService("TweenService")

local camera = game.Workspace.Camera
local studio = game.Workspace.Studio

game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function(pet)
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = studio.CamPart.CFrame
	
	wait(1.5)
	
	for i = 1, 50, 1 do
		studio.EasterEgg.Size = studio.EasterEgg.Size + Vector3.new(0.1,0.1,0.1)
		wait(0.01)
	end
	
	local explosion = Instance.new("Explosion")
	explosion.BlastRadius = 10
	explosion.BlastPressure = 0
	explosion.Position = studio.EasterEgg.Position
	explosion.ExplosionType = Enum.ExplosionType.NoCraters
	explosion.DestroyJointRadiusPercent = 0
	
	explosion.Parent = studio.EasterEgg
	
	studio.EasterEgg.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.PartyMesh:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = true
		end
	end
	
	petClone:SetPrimaryPartCFrame(CFrame.new(studio.EasterEgg.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.PartyMesh:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = false
		end
	end
	
	camera.CameraType = Enum.CameraType.Custom
	studio.EasterEgg.Transparency = 0
	studio.EasterEgg.Size = Vector3.new(4.154, 4.805, 4.52)
	petClone:Destroy()
	
end)

5 comments

1

Is the pet stored in ServerStorage?

0

So i have a folder in ReplicatedStorage called "Pets" all of my pets go in there

0

clone isn't the one that is nil. it says index nil WITH clone so the one before is nil

0

The one before?? Please explain

0

Im not sure what to call it so i said the one before. On line 28, you can see :Clone() is indexing Pet. Pet is the one I called one before and has a nil value

2 answers

✓ Accepted answer

When you fireclientevent, you didn't send in the Pet argument so Pet is nil. Line 14 of script inside model should be

game.ReplicatedStorage.HatchEgg:FireClient(player, pet)

1 comment

0

Yeah i saw that and fixed it

Never mind, This is fix and thanks for the help anyways to everyone who did come and post something

This discussion is preserved as it was. New answers, comments, and votes are closed.