Egg Hatching System Problem

  1. Hi ive been trying to fix my egg hatching system but for some reason it wont work.
    Ive been watching an egg hatching system tutorial and Re-Wrote the code and it still doesnt work.

  2. So when i press the “E” key it works because i put a print, but it doesnt show the hatch animation.

  3. Heres the script, any help will be appreciated!

local replicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local lighting = game:GetService("Lighting")
local tweenService = game:GetService("TweenService")

local module3D = require(replicatedStorage.Libs.Module3D)

local player = Players.LocalPlayer
local PlayerGui = player.PlayerGui

local eggs = workspace:WaitForChild("Eggs")
local pets = replicatedStorage:WaitForChild("Pets")

local BillboardGUI = script.Template

local hatchAnimationGUI = PlayerGui:WaitForChild("HatchAnimation")
local animationMainFrame = hatchAnimationGUI.MainFrame

local container = BillboardGUI:WaitForChild("Container")
local template = container:WaitForChild("MainFrame"):WaitForChild("Template")

local ClicksIMG = "rbxassetid://15725492864"

task.wait(3)

local selectedEgg = nil
local canHatch = false

local function tweenEgg(Obj, t, s, d, Props)
	return tweenService:Create(Obj, TweenInfo.new(t, s, d, 0, false, 0), Props)
end

local function rotateEgg(egg, rotation, t)
	local Cframe = CFrame.new(egg.Position) * CFrame.Angles(0, 0, math.rad(rotation))
	
	local rotateTween = tweenEgg(egg, t, Enum.EasingStyle.Linear, Enum.EasingDirection.In, {CFrame = Cframe})
	rotateTween:Play()
	rotateTween.Completed:Wait()
	rotateTween:Destroy(); rotateTween = nil
end

local function HatchAnim(egg, pet)
	coroutine.wrap(function()
		lighting.AnimationBlur.Enabled = true
		lighting.AnimationBlur.Size = 26
		
		local petModel = pets:WaitForChild(egg):WaitForChild(pet):Clone()
		local eggModel = replicatedStorage.EggModels:WaitForChild(egg):Clone()
		
		if petModel ~= nil and eggModel ~= nil then
			local temp = animationMainFrame.Template:Clone()
			temp.Name = pet
			temp.Parent = animationMainFrame
			temp.Visible = true
			
			local viewportEggModel = module3D:Attach3D(temp, eggModel)
			viewportEggModel:SetDepthMultiplier(1.2)
			viewportEggModel.Camera.FieldOfView = 75
			viewportEggModel.Visible = true
			
			local liftViewport = tweenService:Create(animationMainFrame, TweenInfo.new(0.65), {Position = UDim2.new(0.5, 0, 0.5, 0)})
			liftViewport:Play()
			
			task.wait(1)
			
			local delayTime = 0.11
			
			for inc = 1, 10 do
				rotateEgg(viewportEggModel.Model3D[eggModel.Name], -15, delayTime)
				rotateEgg(viewportEggModel.Model3D[eggModel.Name], 15, delayTime)
				
				delayTime -= 0.007
			end
			
			rotateEgg(viewportEggModel.Model3D[eggModel.Name], 0, delayTime)
			
			for i, obj in pairs(temp:GetChildren()) do
				if obj:IsA("ViewportFrame") then
					obj:Destroy()
				end
			end
			
			temp.PetName.Text = petModel.Name
			temp.PetName.Visible = true
			
			local viewportPetModel = module3D:Attach3D(temp, petModel)
			viewportPetModel:SetDepthMultiplier(1.2)
			viewportPetModel.Camera.FieldOfView = 100
			viewportPetModel.Visible = true
			
			task.wait(2)
			
			local size = lighting.AnimationBlur.Size / 10
			for i = 1, 10 do
				lighting.AnimationBlur.Size -= size
				wait(0.05)
			end
			
			local lowerFrameTween = tweenEgg(animationMainFrame, 0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, { Position = UDim2.new(0.5, 0, 1.5, 0)})
			lowerFrameTween:Play()
			lowerFrameTween.Completed:Wait()
			lowerFrameTween:Destroy()
			
			temp:Destroy()
		end
	end)
end

local function Hatch(hatchAmount)
	if selectedEgg then
		local coins = player.leaderstats.Coins.Value
		local price = eggs:FindFirstChild(selectedEgg):GetAttribute("Price") * hatchAmount
		
		local afford = coins > price
		if afford then
			local hatch = replicatedStorage.Remotes.HatchPets:InvokeServer(selectedEgg)
			if hatch then
				for _, pet in hatch do
					HatchAnim(selectedEgg, pet)
				end
			end
		end
	end
end

for i, eggModel in pairs(eggs:GetChildren()) do
	local petsFolder = pets:FindFirstChild(eggModel.Name)
	if petsFolder ~= nil then
		local clonedBillboard = BillboardGUI:Clone()
		local containerC = clonedBillboard:WaitForChild("Container")
		local mainFrameC = containerC:WaitForChild("MainFrame")
		local templateC = mainFrameC:WaitForChild("Template")
		
		clonedBillboard.Name = eggModel.Name
		clonedBillboard.Adornee = eggModel.Egg
		clonedBillboard.Enabled = true
		clonedBillboard.Parent = PlayerGui.EggBillboards
		
		
		eggModel.PriceIndicator.EggCost.Price.Text = tostring(eggModel:GetAttribute("Price"))
		eggModel.PriceIndicator.EggCost.Currency.Image = if eggModel:GetAttribute("Currency") == "Coins" 
		 then ClicksIMG else ""
		
		for _, pet in pairs(petsFolder:GetChildren()) do
			local newTemp = templateC:Clone()
			
			local petRarity = pet:GetAttribute("Rarity")
			local petRarityName = pet:GetAttribute("RarityName")
			
			
			newTemp.Name = pet.Name
			
			newTemp.Chance.Text = if petRarity > 1 then tostring(petRarity).."%" else "???"
			
			newTemp.Rarity.Text = petRarityName
			newTemp.Rarity.TextColor3 = if petRarityName == "Basic" then Color3.fromRGB(189,189,189)
				elseif petRarityName == "Rare" then Color3.fromRGB(180, 47, 49)
				elseif petRarityName == "Epic" then Color3.fromRGB(204,204,0)
				else Color3.fromRGB(0,0,0)
			
			newTemp.Visible = true
			newTemp.Parent = mainFrameC
			newTemp.LayoutOrder = -petRarity
				
			local spinningPet = module3D:Attach3D(newTemp.ViewportFrame,pet:Clone())
			spinningPet:SetDepthMultiplier(1.2)
			spinningPet.Camera.FieldOfView = 5
			spinningPet.Visible = true
			
			RunService.RenderStepped:Connect(function()
				spinningPet:SetCFrame(CFrame.Angles(0, tick() * 1.25 % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
			end)
			
			
			RunService.RenderStepped:Connect(function()
				if player:DistanceFromCharacter(eggModel.Egg.Position) < 10 then
					for _, billboard in pairs(PlayerGui.EggBillboards:GetChildren()) do
						if billboard.Name == eggModel.Name then
							selectedEgg = eggModel.Name
							billboard.Enabled = true
						else
							billboard.Enabled = false
						end
					end
				end
			end)
		end	
	else
		warn("Their is no pets folder for the egg",eggModel.Name)
	end
end

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if selectedEgg ~= nil and not canHatch then
			canHatch = true
			Hatch(1)
			print("Key Has Been Pressed!")
			
			task.wait(6)
			canHatch = false
		end
	end
end)
2 Likes