Advertisement
Scripting_King

Untitled

Dec 1st, 2023
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.12 KB | Source Code | 0 0
  1. --[[Initializing various services provided by Roblox to interact with the game. ReplicatedStorage, RunService, TweenService, UserInputService, and MarketplaceService are essential for different aspects of game.
  2. ]]--
  3. local replicatedStorage = game:GetService("ReplicatedStorage")
  4. local runService = game:GetService("RunService")
  5. local tweenService = game:GetService("TweenService")
  6. local uis = game:GetService("UserInputService")
  7. local mps = game:GetService("MarketplaceService")
  8. --Gamepass IDs
  9. local tripleHatchID = 24274065
  10. local autoHatchId = 32338581
  11. --Getting local player and their camera
  12. local player = game.Players.LocalPlayer
  13. local camera = workspace.Camera
  14. -- Accessing important assets in game, such as pets and eggs. we also requiring a module for 3D UIs (Viewport).
  15. local Pets = replicatedStorage:WaitForChild("Pets")
  16. local Eggs = workspace:WaitForChild("Eggs")
  17. local Module3D = require(replicatedStorage:WaitForChild("Module3D"))
  18.  
  19. local stopAutoHatching = script.Parent.Parent:WaitForChild("StopAutoHatching")
  20. --These variables controls various states of game, including distances, hatching status, and cooldowns.
  21. local MaxDisplayDistance = 15
  22. local canHatch = false
  23. local isHatching = false
  24. local hatchOneConnection = nil
  25. local cantOpenBillboard = false
  26. local cooldown = false
  27. local IsAutoHatching = false
  28.  
  29. wait(.5)
  30.  
  31. -- Define a function to animate the size of a BillboardGui based on a boolean parameter.
  32. local function animateBillboard(billboard, openOrClose)
  33.     if openOrClose == true then
  34.         -- If openOrClose is true, tween the size to expand the Billboard.
  35.         tweenService:Create(billboard,TweenInfo.new(.1),{Size = UDim2.new(5,0,7,0)}):Play()
  36.     else
  37.     -- If openOrClose is false, tween the size to collapse the Billboard and disable it after a short delay.
  38.         tweenService:Create(billboard,TweenInfo.new(.1),{Size = UDim2.new(0,0,0,0)}):Play()
  39.         wait(.2)
  40.         billboard.Enabled = false
  41.  
  42.     end
  43.     wait(.5)
  44. end
  45.  
  46. -- Define a function to toggle the visibility of screen GUIs.
  47. local function ToggleScreenGuis(bool)
  48.     for i, v in pairs(script.Parent.Parent.Parent:GetChildren()) do
  49.         if v.Name ~= "EggSystem" then
  50.             v.Enabled = bool
  51.         end
  52.     end
  53. end
  54.  
  55. -- Define a function to disable all Billboards by animating them to close.
  56. local function disableAllBillboards()
  57.     cantOpenBillboard = true
  58.     for i, v in pairs(script.Parent.Parent.EggBillboards:GetChildren()) do
  59.         if v:IsA("BillboardGui") then
  60.             animateBillboard(v, false)
  61.         end
  62.     end
  63. end
  64.  
  65. -- Define a function to enable all Billboards by animating them to open.
  66. local function EnableAllBillboards()
  67.     cantOpenBillboard = false
  68.     for i, v in pairs(script.Parent.Parent.EggBillboards:GetChildren()) do
  69.         if v:IsA("BillboardGui") then
  70.             animateBillboard(v, true)
  71.         end
  72.     end
  73. end
  74.  
  75. -- Iterate through each child of the 'Eggs' object.
  76. for i, v in pairs(Eggs:GetChildren()) do
  77.     -- Check if the current egg has corresponding pets in the 'Pets' object.
  78.     local eggPets = Pets:FindFirstChild(v.Name)
  79.  
  80.     if eggPets ~= nil then
  81.     -- Create a BillboardGui template for each egg and configure its contents based on pet rarities.
  82.         local billboardTemp = script.Template:Clone()
  83.         local container = billboardTemp:WaitForChild("Container")
  84.         local MainFrame = container:WaitForChild("MainFrame")
  85.         local template = MainFrame:WaitForChild("Template")
  86.         local display = template:WaitForChild("Display")
  87.  
  88.         billboardTemp.Parent = script.Parent.Parent.EggBillboards
  89.         billboardTemp.Name = v.Name
  90.         billboardTemp.Adornee = v.EggMesh
  91.         billboardTemp.Enabled = true
  92.  
  93.         local pets = {}
  94.  
  95.         for x, pet in pairs(eggPets:GetChildren()) do
  96.             table.insert(pets,pet.Rarity.Value)
  97.         end
  98.  
  99.         table.sort(pets)
  100.  
  101.         for i = 1, math.floor(#pets/2) do
  102.             local j = #pets - i + 1
  103.             pets[i], pets[j] = pets[j], pets[i]
  104.         end
  105.  
  106.         for _, rarity in pairs(pets) do
  107.  
  108.             for _, pet in pairs(eggPets:GetChildren()) do
  109.                 if pet.Rarity.Value == rarity then
  110.                     local rarity = pet.Rarity.Value
  111.                    
  112.                     -- Attach 3D models to the BillboardGui to display pets.
  113.                     local clonedTemp = template:Clone()
  114.  
  115.                     clonedTemp.Name = pet.Name
  116.                     clonedTemp.Rarity.Text = tostring(pet.Rarity.Value).."%"
  117.                     clonedTemp.Visible = true
  118.                     clonedTemp.Parent = MainFrame
  119.  
  120.                     local petModel = Module3D:Attach3D(clonedTemp.Display,pet:Clone())
  121.                     petModel:SetDepthMultiplier(1.2)
  122.                     petModel.Camera.FieldOfView = 5
  123.                     petModel.Visible = true
  124.                    
  125.                     runService.RenderStepped:Connect(function()
  126.                         petModel:SetCFrame(CFrame.Angles(0,tick() * 2 % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
  127.                     end)
  128.  
  129.                     break
  130.                 else
  131.                     continue
  132.                 end
  133.  
  134.             end
  135.         end
  136.  
  137.  
  138.         -- Set up a RenderStepped connection to dynamically show/hide Billboards based on player distance.
  139.         runService.RenderStepped:Connect(function()
  140.             if player:DistanceFromCharacter(v.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
  141.                 if cantOpenBillboard == false then
  142.                     billboardTemp.Enabled = true
  143.                     animateBillboard(billboardTemp, true)
  144.                 end
  145.  
  146.             else
  147.                 if cantOpenBillboard == false then
  148.  
  149.                     animateBillboard(billboardTemp, false)
  150.                 end
  151.             end
  152.         end)
  153.     end
  154. end
  155.  
  156. -- Define a function to handle hatching a single pet from an egg.
  157. local function hatchOne(petName, egg)
  158.     -- Spawn a function to disable all Billboards and toggle screen GUIs during the hatching process.
  159.     spawn(function() disableAllBillboards() end)
  160.     ToggleScreenGuis(false)
  161.     script.Parent.Parent.PetDisplay.PetNameDisplay.Text = petName
  162.     local pet = Pets[egg.Name]:FindFirstChild(petName):Clone()
  163.     isHatching = true
  164.     local eggMesh = egg:FindFirstChild("EggMesh"):Clone()
  165.     for i, v in pairs(eggMesh:GetChildren()) do
  166.         if v:IsA("BasePart") then
  167.             v.Anchored = true
  168.             v.CanCollide = false
  169.         end
  170.     end
  171.    
  172.     -- Set up the 3D display for the hatched pet and animate the hatching process.
  173.     hatchOneConnection = runService.RenderStepped:Connect(function()
  174.         local cf = CFrame.new(0,0,-eggMesh.PrimaryPart.Size.Z * 2) * CFrame.Angles(0,0,math.sin(time() * 18)/2.3)
  175.         eggMesh:SetPrimaryPartCFrame(camera.CFrame * cf)
  176.     end)
  177.     eggMesh.Parent = camera
  178.     print("Hatching")
  179.     wait(3)
  180.     for i, v in pairs(eggMesh:GetChildren()) do
  181.         if v:IsA("BasePart") then
  182.             tweenService:Create(v,TweenInfo.new(.5),{Transparency = 1}):Play()
  183.         end
  184.     end
  185.     wait(.5)
  186.     hatchOneConnection:Disconnect()
  187.     eggMesh:Destroy()
  188.     script.Parent.Parent.PetDisplay.Visible = true
  189.     local petModel = Module3D:Attach3D(script.Parent.Parent.PetDisplay,pet)
  190.     petModel:SetDepthMultiplier(1.2)
  191.     petModel.Camera.FieldOfView = 5
  192.     petModel.Visible = true
  193.  
  194.      -- Enable Billboards and toggle screen GUIs back to their original state.
  195.     runService.RenderStepped:Connect(function()
  196.         petModel:SetCFrame(CFrame.Angles(0,tick() * 2 % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
  197.     end)
  198.     wait(3)
  199.     tweenService:Create(script.Parent.Parent.PetDisplay:FindFirstChildOfClass("ViewportFrame"),TweenInfo.new(.5),{ImageTransparency = 1}):Play()
  200.     wait(.5)
  201.  
  202.     for i, v in pairs(script.Parent.Parent.PetDisplay:GetDescendants()) do
  203.         if v:IsA("ViewportFrame") then
  204.             v:Destroy()
  205.         end
  206.     end
  207.     script.Parent.Parent.PetDisplay.Visible = false
  208.  
  209.     isHatching = false
  210.  
  211.     -- Call a function to handle the creation of a new template for the hatched pet.
  212.     spawn(function() EnableAllBillboards() end)
  213.     ToggleScreenGuis(true)
  214.     _G.newTemplate(petName)
  215.     camera.CameraType = Enum.CameraType.Follow
  216. end
  217.  
  218. -- Define a function to handle hatching three pets from eggs.
  219. local function tripleHatch(petName,petName2,petName3,egg)
  220.      -- Similar structure to the hatchOne function, but for hatching three pets simultaneously.
  221.     spawn(function() disableAllBillboards() end)
  222.     ToggleScreenGuis(false)
  223.  
  224.     local pet = Pets[egg.Name]:FindFirstChild(petName):Clone()
  225.     local pet2 = Pets[egg.Name]:FindFirstChild(petName2):Clone()
  226.     local pet3 = Pets[egg.Name]:FindFirstChild(petName3):Clone()
  227.  
  228.  
  229.     script.Parent.Parent.PetDisplay.PetNameDisplay.Text = petName
  230.     script.Parent.Parent.PetDisplay2.PetNameDisplay.Text = petName2
  231.     script.Parent.Parent.PetDisplay3.PetNameDisplay.Text = petName3
  232.  
  233.     isHatching = true
  234.     local eggMesh = egg:FindFirstChild("EggMesh"):Clone()
  235.     local eggMesh2 = egg:FindFirstChild("EggMesh"):Clone()
  236.     local eggMesh3 = egg:FindFirstChild("EggMesh"):Clone()
  237.  
  238.     for i, v in pairs(eggMesh:GetChildren()) do
  239.         if v:IsA("BasePart") then
  240.             v.Anchored = true
  241.             v.CanCollide = false
  242.         end
  243.     end
  244.     for i, v in pairs(eggMesh2:GetChildren()) do
  245.         if v:IsA("BasePart") then
  246.             v.Anchored = true
  247.             v.CanCollide = false
  248.         end
  249.     end
  250.     for i, v in pairs(eggMesh3:GetChildren()) do
  251.         if v:IsA("BasePart") then
  252.             v.Anchored = true
  253.             v.CanCollide = false
  254.         end
  255.     end
  256.    
  257.     - Set up 3D displays for each pet and animate the hatching process for all three.
  258.     hatchOneConnection = runService.RenderStepped:Connect(function()
  259.         local cf = CFrame.new(0,0,-eggMesh.PrimaryPart.Size.Z * 2) * CFrame.Angles(0,0,math.sin(time() * 18)/2.3)
  260.         local cf2 = CFrame.new(6,0,-eggMesh.PrimaryPart.Size.Z * 2) * CFrame.Angles(0,0,math.sin(time() * 18)/2.3)
  261.         local cf3 = CFrame.new(-6,0,-eggMesh.PrimaryPart.Size.Z * 2) * CFrame.Angles(0,0,math.sin(time() * 18)/2.3)
  262.         eggMesh:SetPrimaryPartCFrame(camera.CFrame * cf)
  263.         eggMesh2:SetPrimaryPartCFrame(camera.CFrame * cf2)
  264.         eggMesh3:SetPrimaryPartCFrame(camera.CFrame * cf3)
  265.  
  266.     end)
  267.     eggMesh.Parent = camera
  268.     eggMesh2.Parent = camera
  269.     eggMesh3.Parent = camera
  270.  
  271.     print("Hatching")
  272.     wait(3)
  273.     for i, v in pairs(eggMesh:GetChildren()) do
  274.         if v:IsA("BasePart") then
  275.             tweenService:Create(v,TweenInfo.new(.5),{Transparency = 1}):Play()
  276.         end
  277.     end
  278.     for i, v in pairs(eggMesh2:GetChildren()) do
  279.         if v:IsA("BasePart") then
  280.             tweenService:Create(v,TweenInfo.new(.5),{Transparency = 1}):Play()
  281.         end
  282.     end
  283.     for i, v in pairs(eggMesh3:GetChildren()) do
  284.         if v:IsA("BasePart") then
  285.             tweenService:Create(v,TweenInfo.new(.5),{Transparency = 1}):Play()
  286.         end
  287.     end
  288.     wait(.5)
  289.     hatchOneConnection:Disconnect()
  290.     eggMesh:Destroy()
  291.     eggMesh2:Destroy()
  292.     eggMesh3:Destroy()
  293.  
  294.     script.Parent.Parent.PetDisplay.Visible = true
  295.     script.Parent.Parent.PetDisplay2.Visible = true
  296.     script.Parent.Parent.PetDisplay3.Visible = true
  297.  
  298.     local petModel = Module3D:Attach3D(script.Parent.Parent.PetDisplay,pet)
  299.     petModel:SetDepthMultiplier(1.2)
  300.     petModel.Camera.FieldOfView = 5
  301.     petModel.Visible = true
  302.  
  303.     local petModel2 = Module3D:Attach3D(script.Parent.Parent.PetDisplay2,pet2)
  304.     petModel2:SetDepthMultiplier(1.2)
  305.     petModel2.Camera.FieldOfView = 5
  306.     petModel2.Visible = true
  307.  
  308.     local petModel3 = Module3D:Attach3D(script.Parent.Parent.PetDisplay3,pet3)
  309.     petModel3:SetDepthMultiplier(1.2)
  310.     petModel3.Camera.FieldOfView = 5
  311.     petModel3.Visible = true
  312.  
  313.     runService.RenderStepped:Connect(function()
  314.         petModel:SetCFrame(CFrame.Angles(0,tick() * 2 % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
  315.         petModel2:SetCFrame(CFrame.Angles(0,tick() * 2 % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
  316.         petModel3:SetCFrame(CFrame.Angles(0,tick() * 2 % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
  317.     end)
  318.     wait(3)
  319.     tweenService:Create(script.Parent.Parent.PetDisplay:FindFirstChildOfClass("ViewportFrame"),TweenInfo.new(.5),{ImageTransparency = 1}):Play()
  320.     tweenService:Create(script.Parent.Parent.PetDisplay2:FindFirstChildOfClass("ViewportFrame"),TweenInfo.new(.5),{ImageTransparency = 1}):Play()
  321.     tweenService:Create(script.Parent.Parent.PetDisplay3:FindFirstChildOfClass("ViewportFrame"),TweenInfo.new(.5),{ImageTransparency = 1}):Play()
  322.  
  323.     wait(.5)
  324.  
  325.     for i, v in pairs(script.Parent.Parent.PetDisplay:GetDescendants()) do
  326.         if v:IsA("ViewportFrame") then
  327.             v:Destroy()
  328.         end
  329.     end
  330.     for i, v in pairs(script.Parent.Parent.PetDisplay2:GetDescendants()) do
  331.         if v:IsA("ViewportFrame") then
  332.             v:Destroy()
  333.         end
  334.     end
  335.     for i, v in pairs(script.Parent.Parent.PetDisplay3:GetDescendants()) do
  336.         if v:IsA("ViewportFrame") then
  337.             v:Destroy()
  338.         end
  339.     end
  340.     script.Parent.Parent.PetDisplay.Visible = false
  341.     script.Parent.Parent.PetDisplay2.Visible = false
  342.     script.Parent.Parent.PetDisplay3.Visible = false
  343.  
  344.  
  345.     isHatching = false
  346.  
  347.     spawn(function() EnableAllBillboards() end)
  348.     ToggleScreenGuis(true)
  349.     _G.newTemplate(petName)
  350.     _G.newTemplate(petName2)
  351.     _G.newTemplate(petName3)
  352.     camera.CameraType = Enum.CameraType.Follow
  353. end
  354.  
  355. -- Set up an input listener for specific key presses (E, R, T) to trigger different hatching actions.
  356. uis.InputBegan:Connect(function(input)
  357.     -- Handle input for hatching a single pet (E key).
  358.     if input.KeyCode == Enum.KeyCode.E then
  359.         if player.Character ~= nil and isHatching == false then
  360.             local nearestEgg
  361.             local plrPos = player.Character.HumanoidRootPart.Position
  362.  
  363.             for i, v in pairs(Eggs:GetChildren()) do
  364.                 if nearestEgg == nil then
  365.                     nearestEgg = v
  366.                 else
  367.                     if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
  368.                         nearestEgg = v
  369.                     end
  370.                 end
  371.             end    
  372.  
  373.             if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
  374.                 canHatch = true
  375.             else
  376.                 canHatch = false
  377.             end
  378.  
  379.             if canHatch == true then
  380.                 local result = replicatedStorage:WaitForChild("EggHatchingRemotes"):WaitForChild("HatchServer"):InvokeServer(nearestEgg)
  381.                 if result ~= "Cannot Hatch" then
  382.                     if not cooldown then
  383.                         cooldown = true
  384.                         hatchOne(result,nearestEgg)
  385.                         wait(.1)
  386.                         cooldown = false
  387.                     end
  388.                 else
  389.                     print("Cannot hatch")
  390.                 end
  391.             end
  392.         end
  393.     end
  394.     -- Handle input for hatching three pets (R key).
  395.     if input.KeyCode == Enum.KeyCode.R then
  396.         if player.Character ~= nil and isHatching == false then
  397.             local nearestEgg
  398.             local plrPos = player.Character.HumanoidRootPart.Position
  399.  
  400.             for i, v in pairs(Eggs:GetChildren()) do
  401.                 if nearestEgg == nil then
  402.                     nearestEgg = v
  403.                 else
  404.                     if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
  405.                         nearestEgg = v
  406.                     end
  407.                 end
  408.             end    
  409.  
  410.             if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
  411.                 canHatch = true
  412.             else
  413.                 canHatch = false
  414.             end
  415.  
  416.             if canHatch == true then
  417.                 local result1, result2, result3 = replicatedStorage.EggHatchingRemotes.Hatch3Pets:InvokeServer(nearestEgg)
  418.  
  419.                 if result1 ~= "The player does not own the gamepass" and result2 ~= nil and result3 ~= nil then
  420.                     if not cooldown then
  421.                         cooldown = true
  422.                         tripleHatch(result1, result2,result3,nearestEgg)
  423.                         wait(.1)
  424.                         cooldown = false
  425.                     end
  426.                 elseif result1 == "The player does not own the gamepass" then
  427.                     mps:PromptGamePassPurchase(player, tripleHatchID)
  428.                 end
  429.             end
  430.         end
  431.     end
  432.  
  433.     -- Handle input for continuously auto-hatching pets until interrupted (T key).
  434.     if input.KeyCode == Enum.KeyCode.T then
  435.         if player.Character ~= nil and isHatching == false then
  436.             local nearestEgg
  437.             local plrPos = player.Character.HumanoidRootPart.Position
  438.  
  439.             for i, v in pairs(Eggs:GetChildren()) do
  440.                 if nearestEgg == nil then
  441.                     nearestEgg = v
  442.                 else
  443.                     if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
  444.                         nearestEgg = v
  445.                     end
  446.                 end
  447.             end    
  448.  
  449.             if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
  450.                 canHatch = true
  451.             else
  452.                 canHatch = false
  453.             end
  454.  
  455.  
  456.             if canHatch == true then
  457.                 IsAutoHatching = true
  458.                 while IsAutoHatching == true do
  459.                     local result = replicatedStorage:WaitForChild("EggHatchingRemotes"):WaitForChild("AutoHatch"):InvokeServer(nearestEgg)
  460.                     if result ~= "Cannot Hatch" and result ~= "The player does not own the gamepass" then
  461.                         if not cooldown then
  462.                             stopAutoHatching.Visible = true
  463.  
  464.                             cooldown = true
  465.                             hatchOne(result,nearestEgg)
  466.                             wait(.1)
  467.                             cooldown = false
  468.                         end
  469.                     elseif result == "The player does not own the gamepass" then
  470.                         mps:PromptGamePassPurchase(player, autoHatchId)
  471.                     end
  472.                     if IsAutoHatching == false then
  473.                         break
  474.                     end
  475.                 end
  476.    
  477.             end
  478.         end
  479.     end
  480. end)
  481.  
  482. -- Iterate through each BillboardGui in the EggBillboards and set up mouse button click events for E, R, and T buttons.
  483. for i, v in pairs(script.Parent.Parent.EggBillboards:GetChildren()) do
  484.     -- Define separate functions for each button click to handle specific hatching actions.
  485.     local Ebtn = v.Container.Buttons.E
  486.     local Rbtn = v.Container.Buttons.R
  487.     local Tbtn = v.Container.Buttons.T
  488.  
  489.     Ebtn.MouseButton1Click:Connect(function()
  490.         if player.Character ~= nil and isHatching == false then
  491.             local nearestEgg
  492.             local plrPos = player.Character.HumanoidRootPart.Position
  493.  
  494.             for i, v in pairs(Eggs:GetChildren()) do
  495.                 if nearestEgg == nil then
  496.                     nearestEgg = v
  497.                 else
  498.                     if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
  499.                         nearestEgg = v
  500.                     end
  501.                 end
  502.             end    
  503.  
  504.             if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
  505.                 canHatch = true
  506.             else
  507.                 canHatch = false
  508.             end
  509.  
  510.             if canHatch == true then
  511.                 local result = replicatedStorage:WaitForChild("EggHatchingRemotes"):WaitForChild("HatchServer"):InvokeServer(nearestEgg)
  512.                 if result ~= "Cannot Hatch" then
  513.                     if not cooldown then
  514.                         cooldown = true
  515.                         hatchOne(result,nearestEgg)
  516.                         wait(.1)
  517.                         cooldown = false
  518.                     end
  519.                 else
  520.                     print("Cannot hatch")
  521.                 end
  522.             end
  523.         end
  524.     end)
  525.  
  526.     Rbtn.MouseButton1Click:Connect(function()
  527.         if player.Character ~= nil and isHatching == false then
  528.             local nearestEgg
  529.             local plrPos = player.Character.HumanoidRootPart.Position
  530.  
  531.             for i, v in pairs(Eggs:GetChildren()) do
  532.                 if nearestEgg == nil then
  533.                     nearestEgg = v
  534.                 else
  535.                     if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
  536.                         nearestEgg = v
  537.                     end
  538.                 end
  539.             end    
  540.  
  541.             if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
  542.                 canHatch = true
  543.             else
  544.                 canHatch = false
  545.             end
  546.  
  547.             if canHatch == true then
  548.                 local result1, result2, result3 = replicatedStorage.EggHatchingRemotes.Hatch3Pets:InvokeServer(nearestEgg)
  549.  
  550.                 if result1 ~= "The player does not own the gamepass" and result2 ~= nil and result3 ~= nil then
  551.                     if not cooldown then
  552.                         cooldown = true
  553.                         tripleHatch(result1, result2,result3,nearestEgg)
  554.                         wait(.1)
  555.                         cooldown = false
  556.                     end
  557.                 elseif result1 == "The player does not own the gamepass" then
  558.                     mps:PromptGamePassPurchase(player, tripleHatchID)
  559.                 end
  560.             end
  561.         end
  562.     end)
  563.    
  564.     Tbtn.MouseButton1Click:Connect(function()
  565.         if player.Character ~= nil and isHatching == false then
  566.             local nearestEgg
  567.             local plrPos = player.Character.HumanoidRootPart.Position
  568.  
  569.             for i, v in pairs(Eggs:GetChildren()) do
  570.                 if nearestEgg == nil then
  571.                     nearestEgg = v
  572.                 else
  573.                     if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
  574.                         nearestEgg = v
  575.                     end
  576.                 end
  577.             end    
  578.  
  579.             if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
  580.                 canHatch = true
  581.             else
  582.                 canHatch = false
  583.             end
  584.  
  585.  
  586.             if canHatch == true then
  587.                 IsAutoHatching = true
  588.                 while IsAutoHatching == true do
  589.                     local result = replicatedStorage:WaitForChild("EggHatchingRemotes"):WaitForChild("AutoHatch"):InvokeServer(nearestEgg)
  590.                     if result ~= "Cannot Hatch" and result ~= "The player does not own the gamepass" then
  591.                         if not cooldown then
  592.                             stopAutoHatching.Visible = true
  593.  
  594.                             cooldown = true
  595.                             hatchOne(result,nearestEgg)
  596.                             wait(.1)
  597.                             cooldown = false
  598.                         end
  599.                     elseif result == "The player does not own the gamepass" then
  600.                         mps:PromptGamePassPurchase(player, autoHatchId)
  601.                     end
  602.                     if IsAutoHatching == false then
  603.                         break
  604.                     end
  605.                 end
  606.             end
  607.         end
  608.     end)
  609. end
  610.  
  611. -- Set up a function to stop the auto-hatching process when the stopAutoHatching button is clicked.
  612. stopAutoHatching.MouseButton1Click:Connect(function()
  613.     IsAutoHatching = false
  614.     stopAutoHatching.Visible = false
  615. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement