Hello! I have multiple scripts for an egg hatching pet system where if you click on a part a random pet will be selected. However, clicking it comes up with an error attempt to get length of a nil value, though, oddly, it works after a few more clicks.
After the pet is selected there's a cut scene of the egg "hatching" where it grows and then explodes, and then the selected pet is cloned to the egg's position. However, the pet doesn't clone and comes up with an error attempt to index nil with 'Clone'. I can't figure out why this is happening.
This is a module script, located in ServerScriptService, which determines the rarities and chooses the pet (attempt to get length of a nil value error occurs here):
local ultrararePetModule = {} ultrararePetModule.pets = { ["Mythical"] = { game.ReplicatedStorage.Pets.Mythical["Demonic Dominus"]; game.ReplicatedStorage.Pets.Mythical["Lava Lord"]; game.ReplicatedStorage.Pets.Mythical["Shadow Dominus"]; game.ReplicatedStorage.Pets.Mythical.Ultimus; }; ["Legendary"] = { game.ReplicatedStorage.Pets.Legendary.Destroyer; game.ReplicatedStorage.Pets.Legendary["Gigantic Lord"]; game.ReplicatedStorage.Pets.Legendary.Kraken; game.ReplicatedStorage.Pets.Legendary["Lava Wizard"]; game.ReplicatedStorage.Pets.Legendary["Robot Overlord"]; game.ReplicatedStorage.Pets.Legendary["Super Slime"]; }; ["UltraRare"] = { game.ReplicatedStorage.Pets.UltraRare["Creature of Light"]; game.ReplicatedStorage.Pets.UltraRare["Electric Spider"]; game.ReplicatedStorage.Pets.UltraRare["Hallows Spider"]; game.ReplicatedStorage.Pets.UltraRare["Lava Beast"]; game.ReplicatedStorage.Pets.UltraRare.OctoHex; game.ReplicatedStorage.Pets.UltraRare["Psychic Scorpion"]; game.ReplicatedStorage.Pets.UltraRare.Storm; }; ["Rare"] = { game.ReplicatedStorage.Pets.Rare.Bat; game.ReplicatedStorage.Pets.Rare.Duck; game.ReplicatedStorage.Pets.Rare["Light Demon"]; game.ReplicatedStorage.Pets.Rare["Mad Scientist"]; game.ReplicatedStorage.Pets.Rare.Monster; game.ReplicatedStorage.Pets.Rare.Robot; game.ReplicatedStorage.Pets.Rare.Starry; game.ReplicatedStorage.Pets.Rare.TV; }; ["Uncommon"] = { game.ReplicatedStorage.Pets.Uncommon.Bee; game.ReplicatedStorage.Pets.Uncommon.Crab; game.ReplicatedStorage.Pets.Uncommon.Monkey; game.ReplicatedStorage.Pets.Uncommon.Ninja; game.ReplicatedStorage.Pets.Uncommon.Pumpkin; game.ReplicatedStorage.Pets.Uncommon.Reindeer; game.ReplicatedStorage.Pets.Uncommon["Skeleton Pup"]; game.ReplicatedStorage.Pets.Uncommon.Turkey; }; ["Common"] = { game.ReplicatedStorage.Pets.Common.Bear; game.ReplicatedStorage.Pets.Common.Bunny; game.ReplicatedStorage.Pets.Common.Cat; game.ReplicatedStorage.Pets.Common.Cow; game.ReplicatedStorage.Pets.Common.Dog; game.ReplicatedStorage.Pets.Common.Fox; game.ReplicatedStorage.Pets.Common.Koala; game.ReplicatedStorage.Pets.Common.Mouse; game.ReplicatedStorage.Pets.Common["Little Dragon"]; }; } ultrararePetModule.rarities = { ["Mythical"] = 1; ["Legendary"] = 9; ["Ultra Rare"] = 60; ["Rare"] = 25; ["Uncommon"] = 5; ["Common"] = 0; } ultrararePetModule.chooseRandomPet = function() local randomNumber = math.random(1,100) local counter = 0 for rarity, weight in pairs(ultrararePetModule.rarities) do counter = counter + weight if randomNumber <= counter then local rarityTable = ultrararePetModule.pets[rarity] local chosenPet = rarityTable[math.random(1, #rarityTable)] -- attempt to get length of nil value error occurs here return chosenPet end end end return ultrararePetModule
This is the script which selects the pet and fires the cutscene when the parent part is clicked:
local petModule = require(game.ServerScriptService:WaitForChild("UltraRareModuleScript")) script.Parent.ClickDetector.MouseClick:Connect(function(player) if player.Currency.Value >= 500 then player.Currency.Value = player.Currency.Value - 500 local pet = petModule.chooseRandomPet() print(pet.Name.." selected") game.ReplicatedStorage.HatchEgg:FireClient(player) end end)
This is the local script, located in StarterGUI, which triggers the cut scene when the client is fired (attempt to index nil with 'Clone' error occurs here):
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.Egg.Size = studio.Egg.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.Egg.Position explosion.ExplosionType = Enum.ExplosionType.NoCraters explosion.DestroyJointRadiusPercent = 0 explosion.Parent = studio.Egg studio.Egg.Transparency = 1 local petClone = pet:Clone() -- attempt to index nil with 'Clone' error occurs here for i, v in pairs(petClone:GetChildren()) do if v:IsA("BasePart") then v.Anchored = true end end for i, v in pairs(studio.Plasma:GetChildren()) do if v:IsA("ParticleEmitter") then v.Enabled = true end end petClone:SetPrimaryPartCFrame(CFrame.new(studio.Egg.Position,studio.CamPart.Position)) petClone.Parent = studio local tweenInfo = TweenInfo.new( 2, Enum.EasingStyle.Quad, 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.Plasma:GetChildren()) do if v:IsA("ParticleEmitter") then v.Enabled = false end end camera.CameraType = Enum.CameraType.Custom studio.EggTransparency = 0 studio.Egg.Size = Vector3.new(3.914, 5, 3.914) petClone:Destroy() end)
Thanks in advance!
The attempt to get length of a nil value error could be because you are trying to index the pets table with the "Ultra Rare" key from the rarities table, but there is no suck key in the pets table, as it is "UltraRare" in the pets table instead of "Ultra Rare".
This would also explain why it would work sometimes.
I solved the clone error myself, the problem was with the second script, I wasn't sending over the "pet" argument over to the client.
local petModule = require(game.ServerScriptService:WaitForChild("UltraRareModuleScript")) script.Parent.ClickDetector.MouseClick:Connect(function(player) if player.leaderstats.Cash.Value >= 500 then player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 500 local pet = petModule.chooseRandomPet() print(pet.Name.." selected") game.ReplicatedStorage.HatchEgg:FireClient(player,pet) end end)
However, I still have an issue with attempt to get length of a nil value.