How to correctly introduce an Auto Hatch and Triple Hatch system in this script?

The problem is that is prompting me to buy the gamepass every time I press the triple hatch or the auto hatch button!

For a commission someone gave me these two script and my job is to make the triple hatch and the auto hatch system work when you have an certain gamepass , so I’ve tried to make it work for two hours now but no luck I was wondering if y’all can help me

First “Client” Module Script:

local u1 = { "", "K", "M", "B", "T", "Qd", "Qn", "sx", "Sp", "O", "N", "D", "Ud", "DD", "tdD", "qdD", "QnD", "sxD", "SpD", "OcD", "NvD", "Vgn", "UVg", "DVg", "TVg", "qtV", "QnV", "SeV", "SPG", "OVG", "NVG", "TGN", "UTG", "DTG", "tsTG", "qtTG", "QnTG", "ssTG", "SpTG", "OcTG", "NoAG", "UnAG", "DuAG", "TeAG", "QdAG", "QnAG", "SxAG", "SpAG", "OcAG", "NvAG", "CT" };
local function v1(p1)
	for v2 = 1, #u1 do
		if tonumber(p1) < 10 ^ (v2 * 3) then
			return math.floor(p1 / (10 ^ ((v2 - 1) * 3) / 100)) / 100 .. u1[v2];
		end;
	end;
end;

local EggHandler = {}

--// Services 
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local MarketPlaceService = game:GetService("MarketplaceService")

--// Variables
repeat wait() until Players.LocalPlayer
local Player = Players.LocalPlayer
local PlayerGUI = Player:WaitForChild("PlayerGui")
local MainUI = PlayerGUI:WaitForChild("MainUI")
local eggUIFolder = MainUI:WaitForChild("EggUI") 
local AutoDeleteFrame = MainUI:WaitForChild("AutoDeleteFrame") 
local eggFolder = Workspace:WaitForChild("Eggs")
local GameClient = ReplicatedStorage:WaitForChild("GameClient")
local Assets = GameClient:WaitForChild("Assets")
local UIClones = Assets:WaitForChild("UIClones")
local BuyEggEvent = GameClient.Events.RemoteFunction.BuyEgg
local DisplayEggOpeningEvent = GameClient.Events.RemoteEvent.DisplayEggOpening
local buyD, lastEgg, clickD, eggs = false, nil, false, {}
local autoOn, currentAutoEgg, waitD = false, nil, false
local RequiredDistance = 20

--// Modules
local ShowPetsInEggsMod = require(script.ShowPetsInEggs)
local EggService = require(script.EggService)
local EggStats = require(GameClient.Modules.Utilities.EggStats)
local PetStats = require(GameClient.Modules.Utilities.PetStats)
local IndexUIModule = require(script.Parent.IndexUIModule)
local AutoDeleteModule = require(script.AutoDeleteModule)

--// Eggs
local c = eggFolder:GetChildren()
for _, v in pairs(c) do
	if v:IsA("Model") then
		eggs[v.Name] = v
	end
end

--// Get Closest Egg 
function EggHandler:GetClosestEgg(distance)
	local Distance = distance ~= nil and distance or RequiredDistance
	local Closest = nil
	for _, v in pairs(eggs) do
		local cameraPart = v:FindFirstChild("View")
		if cameraPart ~= nil then
			local partPOS = cameraPart.Position
			local newDistance = Player:DistanceFromCharacter(partPOS)
			if newDistance <= Distance then
				Closest = v
				Distance = newDistance
			end
		end
	end
	return Closest
end

--// Get Egg Stats
function EggHandler:GetEggStatsForEgg(eggName)
	if EggStats[eggName] == nil then
		warn("No Egg Stats For \"" .. tostring(eggName) .. "\"")
		return EggStats.Common
	end
	return EggStats[eggName]
end

function EggHandler:GetEggCostCurrency(eggName)
	local stats = self:GetEggStatsForEgg(eggName)
	local cost = stats.Cost
	
	if cost ~= nil and cost[1] ~= nil and cost[2] ~= nil then
		return tonumber(cost[2]), tostring(cost[1])
	end
	
	return 0, "Coins"
end

--// Check Pass
function HasPass(id)
	local mp = game:GetService("MarketplaceService")	
	if mp:UserOwnsGamePassAsync(Player.UserId,id) then
		return true
	else
		return false
	end
end

function CheckPass(passName, which)
	if passName == "ThreeEggOpen" then
		local id = 103483280
		if which == "Check" then
			return true
		elseif which == "Prompt" then
			if HasPass(id) == false then 
				MarketPlaceService:PromptGamePassPurchase(Player, id)
			end
		end
	elseif passName == "AutoHatch" then
		local id = 103112632
		if which == "Check" then
			return true
		elseif which == "Prompt" then
			if HasPass(id) == false then 
				MarketPlaceService:PromptGamePassPurchase(Player, id)
			end
		end
	end
	return false
end

--// Buy Egg
function EggHandler:BuyEgg(egg, auto)
	if MainUI:FindFirstChild("PetHatch") == nil and MainUI:FindFirstChild("MultiPetHatch") == nil then
		if buyD == false and AutoDeleteFrame.Visible == false then
			buyD = true
			local eggName = egg.Name
			local stats = self:GetEggStatsForEgg(eggName)
			local cost,currency = EggHandler:GetEggCostCurrency(eggName)
			if currency == "Robux" and stats.ProductID ~= nil and stats.ProductID2 ~= nil then
				local productID = stats.ProductID
				MarketPlaceService:PromptProductPurchase(Player, productID)
			else
				local pet = BuyEggEvent:InvokeServer(eggName, "Buy1")
				if pet == "Max" then
					print("Max Pets!")
					local msg = "You Backpack full!"
					Player.PlayerGui.Fotma.MaxPets.Visible = true
					wait(1.5)
					Player.PlayerGui.Fotma.MaxPets.Visible = false
					buyD = false return true
				elseif pet == "GetClose" then
					print("Get Closer To Buy Egg!")
					buyD = false return true
				elseif pet == "Locked" then
					print("Egg Is Locked! Unlock The Correct Area!")
					buyD = false return true
				elseif pet then
					if pet[1] == "NeedMoney" then
						local currencyName,amountNeeded = pet[2],pet[3]
						local msg = "You do not have enough gems to purchase this egg!"
						print("Not Enough Coins!")
						Player.PlayerGui.Fotma.NoCoins.Visible = true
						wait(1.5)
						Player.PlayerGui.Fotma.NoCoins.Visible = false
					else
						local petName, newPet, shiny, autoDeleted = pet[1], pet[2], pet[3], pet[4]
						ShowPetsInEggsMod:ChangePetPercentages(Player, eggs) IndexUIModule:UpdateIndex()
						EggService:HatchEgg(eggName, petName, newPet, nil, nil, shiny, autoDeleted, auto)
					end
				end
			end
			wait() buyD = false
		end
	end
end

function EggHandler:Buy3Eggs(egg)
	if MainUI:FindFirstChild("PetHatch") == nil and MainUI:FindFirstChild("MultiPetHatch") == nil then
		if buyD == false and AutoDeleteFrame.Visible == false then
			buyD = true
			if CheckPass("ThreeEggOpen", "Check") == true then
				local eggName = egg.Name
				local stats = self:GetEggStatsForEgg(eggName)
				local cost,currency = EggHandler:GetEggCostCurrency(eggName)
				if currency == "Robux" and stats.ProductID ~= nil and stats.ProductID2 ~= nil then
					local productID = stats.ProductID2
					MarketPlaceService:PromptProductPurchase(Player, productID)
				else
					local pet1, pet2, pet3 = BuyEggEvent:InvokeServer(eggName, "Buy3")
					if pet1 == "Max" then
						print("Max Pets!")
						buyD = false return true
					elseif pet1 == "GetClose" then
						print("Get Closer To Buy Egg!")
						buyD = false return true
					elseif pet1 == "NeedPass" then
						CheckPass("ThreeEggOpen", "Prompt")
						buyD = false return true
					elseif pet1 == "Locked" then
						print("Egg Is Locked! Unlock The Correct Area!")
						buyD = false return true
					else
						if pet1[1] == "NeedMoney" then
							local currencyName,amountNeeded = pet1[2],pet1[3]
							local msg = "You do not have enough gems to purchase three of these eggs!"
							print("Not Enough Coins!")
						elseif pet1 ~= nil and pet2 ~= nil and pet3 ~= nil then
							ShowPetsInEggsMod:ChangePetPercentages(Player, eggs)
							IndexUIModule:UpdateIndex()
							EggService:MultiHatchEgg(eggName, {pet1, pet2, pet3,})
						end
					end
				end
			else
				CheckPass("ThreeEggOpen", "Prompt")
			end
			wait() buyD = false
		end
	end
end

function EggHandler:AutoOpenEgg()
	if CheckPass("AutoHatch", "Check") == true then
		local function loopCancel()
			autoOn = false	
			currentAutoEgg = nil
		end
		local function autoOpenOne()
			autoOn = true
			spawn(function()
				while true do
					if autoOn == false then
						loopCancel() break
					end
					if AutoDeleteFrame.Visible == true then
						loopCancel() break
					end
					local closestEgg = EggHandler:GetClosestEgg(10)
					if closestEgg ~= nil then
						if currentAutoEgg == nil then 
							currentAutoEgg = closestEgg.Name 
						end
						if currentAutoEgg == closestEgg.Name then
							currentAutoEgg = closestEgg.Name
							local maxPets = EggHandler:BuyEgg(closestEgg, true)
							if maxPets == true then
								loopCancel() 
								break
							end
							repeat wait() 
							until MainUI:FindFirstChild("MultiPetHatch") == nil and MainUI:FindFirstChild("PetHatch") == nil
							wait(1)
						else
							loopCancel() break
						end
					else
						loopCancel() break
					end
				end
			end)
		end
		local function autoOpenThree()
			if CheckPass("ThreeEggOpen", "Check") == true then
				autoOn = true
				spawn(function()
					while true do
						if autoOn == false then
							loopCancel() break
						end
						if AutoDeleteFrame.Visible == true then
							loopCancel() break
						end
						local closestEgg = EggHandler:GetClosestEgg(10)
						if closestEgg ~= nil then
							if currentAutoEgg == nil then 
								currentAutoEgg = closestEgg.Name 
							end
							if currentAutoEgg == closestEgg.Name then
								currentAutoEgg = closestEgg.Name
								local maxPets = EggHandler:Buy3Eggs(closestEgg, true)
								if maxPets == true then
									loopCancel() break
								end
								repeat wait() 
								until MainUI:FindFirstChild("MultiPetHatch") == nil and MainUI:FindFirstChild("PetHatch") == nil
								wait(1)
							else
								loopCancel() break
							end
						else
							loopCancel() break
						end
					end
				end)
			else
				loopCancel()
			end
		end
		if autoOn == true then return end
		if CheckPass("ThreeEggOpen", "Check") == true then
			autoOpenThree()
		else
			autoOpenOne()
		end
	else
		autoOn, currentAutoEgg = false, nil
		CheckPass("AutoHatch", "Prompt")
	end
end

function EggHandler:OpenSpecialEgg(eggName, which, pets)
	if eggName ~= nil and which ~= nil and pets ~= nil then
		delay(1, function()
			if which == "Open3" then
				local pet1, pet2, pet3 = pets[1],pets[2],pets[3]
				if pet1 == "Max" then
					print("Max Pets!")
					return true
				elseif pet1 and pet2 and pet3 then
					EggService:MultiHatchEgg(eggName, {pet1, pet2, pet3,})
				end
			elseif which == "Open1" then
				local pet = pets
				if pet == "Max" then
					print("Max Pets!")
					return true
				elseif pet then
					local petName,newPet,shiny = pet[1],pet[2],pet[3]
					EggService:HatchEgg(eggName, petName, newPet, nil, nil, shiny, false, nil)
				end	
			end
		end)
	end
end

function EggHandler:Int()
	for _, v in pairs(eggs) do
		local eggName = v.Name
		if eggUIFolder:FindFirstChild(eggName) == nil then
			local EggUI = UIClones.EggUI:Clone()
			EggUI.Name = eggName
			
			local cost, currency = self:GetEggCostCurrency(eggName)
			if tonumber(cost) <= 0 then
				EggUI.Frame.Cost.AmountText.Text = "Free"
			else
				EggUI.Frame.Cost.AmountText.Text = v1(tostring(cost))
			end
			
			if tostring(currency) == "Robux" then
				EggUI.Frame.Cost.Icon.Image = "rbxassetid://5470193133"
			elseif tostring(currency) == "Coins" then
				EggUI.Frame.Cost.Icon.Image = "rbxassetid://363483133"
			elseif tostring(currency) == "Tickets" then
				EggUI.Frame.Cost.Icon.Image = "rbxassetid://2561026986"
			end
			
			local cameraPart = v:FindFirstChild("View")
			EggUI.Adornee = cameraPart
			
			if tostring(currency) == "Robux" then
				EggUI.Frame.Single.Click.MouseButton1Click:Connect(function() 
					self:BuyEgg(v) 
				end)
				EggUI.Frame.Triple.Click.MouseButton1Click:Connect(function() 
					self:Buy3Eggs(v)
				end)
				
				EggUI.Frame.Auto.Visible, EggUI.Frame.AutoDelete.Visible = false, false
				EggUI.Frame.Single.Position = UDim2.new(0.5, 0, 0.915, 0)
				EggUI.Frame.Triple.Position = UDim2.new(0.7, 0, 0.915, 0)
			else
				EggUI.Frame.Single.Click.MouseButton1Click:Connect(function() 
					self:BuyEgg(v) 
				end)
				EggUI.Frame.Triple.Click.MouseButton1Click:Connect(function() 
					self:Buy3Eggs(v)
				end)
				EggUI.Frame.Auto.Click.MouseButton1Click:Connect(function()
					self:AutoOpenEgg()
				end)
				EggUI.Frame.AutoDelete.Click.MouseButton1Click:Connect(function()
					AutoDeleteModule:OpenAutoDelete(eggName)
				end)
			end
			
			EggUI.Enabled = false
			EggUI.Parent = eggUIFolder
		end
	end
	
	UserInputService.InputBegan:Connect(function(input, gp)
		if gp then return end
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if clickD == false then
				if input.KeyCode == Enum.KeyCode.E then
					clickD = true
					local closestEgg = self:GetClosestEgg()
					if closestEgg ~= nil then
						self:BuyEgg(closestEgg)
					end
					wait() clickD = false
				elseif input.KeyCode == Enum.KeyCode.R then
					clickD = true
					local closestEgg = self:GetClosestEgg()
					if closestEgg ~= nil then
						self:Buy3Eggs(closestEgg)
					end
					wait() clickD = false
				elseif input.KeyCode == Enum.KeyCode.T then
					clickD = true
					local closestEgg = self:GetClosestEgg()
					if closestEgg then
						local stats = self:GetEggStatsForEgg(tostring(closestEgg))
						local cost, currency = self:GetEggCostCurrency(tostring(closestEgg))
						if currency == "Robux" and stats.ProductID ~= nil and stats.ProductID2 ~= nil then 
						else
							self:AutoOpenEgg()
						end
					end
					wait() clickD = false
				end
			end
		end
	end)
	
	AutoDeleteModule:Int()
	
	ShowPetsInEggsMod:LoadPets(Player, eggs)
	
	DisplayEggOpeningEvent.OnClientEvent:Connect(function(...)
		self:OpenSpecialEgg(...)
	end)
	
	spawn(function()
		RunService.RenderStepped:Connect(function()
			local closestEgg = self:GetClosestEgg()
			if closestEgg then
				if closestEgg ~= lastEgg then
					lastEgg = closestEgg
					for _, frame in pairs(eggUIFolder:GetChildren()) do
						if frame.Name == tostring(closestEgg) then
							frame.Enabled = true
						else
							frame.Enabled = false
						end
					end
				end
			else
				if waitD == false then
					waitD = true
					lastEgg = nil
					for _, frame in pairs(eggUIFolder:GetChildren()) do
						frame.Enabled = false
					end
					delay(1, function() waitD = false end)
				end
			end
		end)
	end)
end

return EggHandler

and the second “Server” Module Script:

local EggHandler = {}

--// Services 
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MarketPlaceService = game:GetService("MarketplaceService")

--// Variables
local DataFolder = ServerStorage.Data
local GameClient = ReplicatedStorage:WaitForChild("GameClient")
local EventsFolder = GameClient:WaitForChild("Events")
local EggEvent = EventsFolder.RemoteFunction.BuyEgg
local AutoDeleteEvent = EventsFolder.RemoteFunction.AutoDelete
local GetAutoDeleteDataEvent = EventsFolder.RemoteFunction.GetAutoDeleteData
local DisplayEggOpeningEvent = EventsFolder.RemoteEvent.DisplayEggOpening
local eggFolder = Workspace:WaitForChild("Eggs")
local shinyRNG = Random.new()
local RequiredDistance = 20

--// Modules
local DataController = require(DataFolder.DataController)
local RandomizeTablesMod = require(GameClient.Modules.UI.RandomizeTables)
local EggStats = require(GameClient.Modules.Utilities.EggStats)
local PetStats = require(GameClient.Modules.Utilities.PetStats)
local LegendaryPetChatModule = require(script.LegendaryPetChat)
local ServerChatMessagesModule = require(script.ServerChatMessages)
local PetHandler = require(script.Parent.PetHandler)
local AutoDeleteServerModule = require(script.AutoDeleteServer)

--// Check Pass
function HasPass(Player, id)
	local mp = game:GetService("MarketplaceService")	
	if mp:UserOwnsGamePassAsync(Player.UserId,id) then
		return true
	else
		return false
	end
end

function CheckPass(Player, passName, which)
    if passName == "ThreeEggOpen" then
		local id = 103483280
        if which == "Check" then
            return HasPass(Player, id)
        elseif which == "Prompt" then
            if HasPass(Player, id) == false then 
                MarketPlaceService:PromptGamePassPurchase(Player, id)
            end
        end
    elseif passName == "AutoHatch" then
		local id = 103112632
        if which == "Check" then
            return HasPass(Player, id)
        elseif which == "Prompt" then
            if HasPass(Player, id) == false then 
                MarketPlaceService:PromptGamePassPurchase(Player, id)
            end
        end
    end
    return false
end

function EggHandler:GetEggCostCurrency(eggName)
    if EggStats[eggName] ~= nil then
        local cost = EggStats[eggName].Cost
        return tonumber(cost[2]), tostring(cost[1])
    end

    warn("No egg cost and no egg currency for \"" .. tostring(eggName) .. "\"")
    return 0, "Gem"
end

function EggHandler:CheckEggDistance(plr, eggName, distance)
    local eggModel = eggFolder:FindFirstChild(eggName)
    if eggModel ~= nil then
        local cameraPart = eggModel:FindFirstChild("View")
        if cameraPart ~= nil then
            local partPOS = cameraPart.Position
            local newDistance = plr:DistanceFromCharacter(partPOS)
            local Distance = distance ~= nil and distance or RequiredDistance
            if newDistance < Distance then
                return true
            end
        end
    end
    return false
end

function EggHandler:OpenEgg(plr, eggName, which)
    if plr and eggName ~= nil and which ~= nil then
        if which == "OnePet" then
            local pet = RandomizeTablesMod.Get1Pet(plr, eggName)
            return pet
        elseif which == "ThreePets" then
            local pet1,pet2,pet3 = RandomizeTablesMod.Get3Pets(plr, eggName)
            return pet1,pet2,pet3
        end
    end
end

function EggHandler:ReturnShinyChance(plr, eggName)
    local shiny_max = 200
    local hasShinyBoost = false -- has shiny boost (5x)
    if hasShinyBoost == true then
        shiny_max = 40 -- 5x more chance for shiny
    end
    return shinyRNG:NextInteger(1, shiny_max) == 1
end

function EggHandler:AddTotalEggAmount(Player, amount)
    amount = tonumber(amount) or 1
    local Data = DataController:GetAllData(Player)
    if Data.PetControl.TotalEggsOpened == nil then
        Data.PetControl.TotalEggsOpened = amount
    else
        local newVal = Data.PetControl.TotalEggsOpened + amount
        Data.PetControl.TotalEggsOpened = tonumber(newVal)
    end
    DataController:SavePlayerData(Player)
end

function EggHandler:GetPlayerCurrency(Player, currency)
    local Data = DataController:GetAllData(Player)
    local amount = 0 -- path to currency
    return tonumber(amount)
end

function EggHandler:RemovePlayerCurrency(Player, currency, amount)
    amount = tonumber(amount) or 0
    local Data = DataController:GetAllData(Player)
    -- remove stat from data
end

function EggHandler:GetCurrentPetAmount(Data)
    local pets = Data.OwnedItems.Pets
    local amount, amountEquipped = 0, 0
    for _, v in pairs(pets) do
        if type(v) == "table" then
            if v.PetIsEquip == true then
                amountEquipped = amountEquipped + 1
            end
            amount = amount + 1
        end
    end
    return amount, amountEquipped
end

function EggHandler:CheckPetInv(Player, which, robuxEgg)
    if robuxEgg == true then return true end
    local amountToAdd = which == "Buy3" and 3 or 1

    local Data = DataController:GetAllData(Player)
    local petAmount = self:GetCurrentPetAmount(Data)
    local MaxPetsInventory = Data.PetControl.MaxPetsInventory or math.huge

    if tonumber(petAmount) == MaxPetsInventory then
        return false
    elseif (tonumber(petAmount) + tonumber(amountToAdd)) > MaxPetsInventory then
        return false
    elseif tonumber(petAmount) < MaxPetsInventory then
        return true
    end
    return false
end

function EggHandler:CheckIfCanOpenEgg(plr, eggName)
    if plr and eggName ~= nil then
        -- add a security to the egg, like check if the player has a world/land/area
        return true
    end 
    return false
end

--// Find Pet In Index
local findPetInIndex = function(Data, PetName)
    local petIndex = Data.OwnedItems.PetIndex or {}
    for _, name in pairs(petIndex) do
        if tostring(name) == tostring(PetName) then
            return true
        end
    end
    return false
end

--// Add Pet To Index
function EggHandler:AddPetToIndex(Player, PetName)
    local Data = DataController:GetAllData(Player)
    local petIndex = Data.OwnedItems.PetIndex
    if petIndex == nil then
        Data.OwnedItems.PetIndex = {}
        DataController:SavePlayerData(Player) wait()
        petIndex = Data.OwnedItems.PetIndex
    end
    if findPetInIndex(Data, tostring(PetName)) == false then
        table.insert(petIndex, tostring(PetName))
        DataController:SavePlayerData(Player)
        return true
    end
    return false
end

--// Get Pet Stats
local function GetPetStats(name)
    local rName = tostring(name)
    if PetStats[rName] == nil then
        warn("No Pet Stats for pet with name of", tostring(name))
        return PetStats.Dog
    end
    return PetStats[rName]
end

--// Check If Pet Auto Deletes
function EggHandler:CheckAutoDeletePet(Player, PetName)
    local AutoDeleteSettings = AutoDeleteServerModule:GetPlayerData(Player)
    local stats = GetPetStats(tostring(PetName))
    local rarity = stats.Rarity or "Common"
    if AutoDeleteSettings[tostring(rarity)] ~= nil then
        if AutoDeleteSettings[tostring(rarity)] == true then
            return true
        else
            local foundInTbl = (function()
                local newPetName = string.upper(tostring(PetName))
                for _, v in pairs(AutoDeleteSettings.Pets) do
                    local name = string.upper(tostring(v))
                    if tostring(name) == tostring(newPetName) then
                        return true
                    end
                end
                return false
            end)()
            if foundInTbl == true then
                return true
            end
        end
    else
        warn("No Auto Delete Settings For ".. tostring(rarity).." Rarity!")
    end
    return false
end

--// Open Robux Egg
function EggHandler:OpenSpecialEgg(plr, eggName, which)
    if plr and eggName ~= nil and which ~= nil then
        if self:CheckPetInv(plr, which, true) == true then
            if which == "Buy1" then
                local pet = self:OpenEgg(plr, tostring(eggName), "OnePet")
                if pet ~= nil then
                    self:AddTotalEggAmount(plr, 1)

                    LegendaryPetChatModule:SendMessage(plr, "Pet", tostring(pet), tostring(eggName))

                    local petTable = {}
                    local new = self:AddPetToIndex(plr, tostring(pet))
                    local shiny = self:ReturnShinyChance(plr, tostring(eggName))
                    if shiny == true then
                        PetHandler:AddPet(plr, {PetName = tostring(pet), Evolution = "Shiny";})
                        petTable = {("Shiny ".. tostring(pet)), new, true, false}
                    else
                        PetHandler:AddPet(plr, {PetName = tostring(pet)})
                        petTable = {pet, new, false, false}
                    end

                    spawn(function()
                        DisplayEggOpeningEvent:FireClient(plr, eggName, "Open1", petTable)
                    end)
                    return true
                end
            else
                local pet1, pet2, pet3 = self:OpenEgg(plr, eggName, "ThreePets")
                if pet1 ~= nil and pet2 ~= nil and pet3 ~= nil then
                    self:AddTotalEggAmount(plr, 3)

                    local new1 = self:AddPetToIndex(plr, tostring(pet1))
                    local new2 = self:AddPetToIndex(plr, tostring(pet2))
                    local new3 = self:AddPetToIndex(plr, tostring(pet3))

                    LegendaryPetChatModule:SendMessage(plr, "Pet", tostring(pet1), tostring(eggName))
                    LegendaryPetChatModule:SendMessage(plr, "Pet", tostring(pet2), tostring(eggName))
                    LegendaryPetChatModule:SendMessage(plr, "Pet", tostring(pet3), tostring(eggName))

                    local tbl1, tbl2, tbl3 = {}, {}, {}

                    local shiny1 = self:ReturnShinyChance(plr, eggName)
                    if shiny1 == true then
                        PetHandler:AddPet(plr, {PetName = tostring(pet1), Evolution = "Shiny";})
                        tbl1 = {("Shiny ".. tostring(pet1)), new1, true, false}
                    else
                        PetHandler:AddPet(plr, {PetName = tostring(pet1)})
                        tbl1 = {tostring(pet1), new1, false, false}
                    end

                    local shiny2 = self:ReturnShinyChance(plr, eggName)
                    if shiny2 == true then
                        PetHandler:AddPet(plr, {PetName = tostring(pet2), Evolution = "Shiny";})
                        tbl2 = {("Shiny ".. tostring(pet2)), new2, true, false}
                    else
                        PetHandler:AddPet(plr, {PetName = tostring(pet2)})
                        tbl2 = {tostring(pet2), new2, false, false}
                    end

                    local shiny3 = self:ReturnShinyChance(plr, eggName)
                    if shiny3 == true then
                        PetHandler:AddPet(plr, {PetName = tostring(pet3), Evolution = "Shiny";})
                        tbl3 = {("Shiny ".. tostring(pet3)), new3, true, false}
                    else
                        PetHandler:AddPet(plr, {PetName = tostring(pet3)})
                        tbl3 = {tostring(pet3), new3, false, false}
                    end

                    local petTable = {tbl1, tbl2, tbl3}

                    spawn(function()
                        DisplayEggOpeningEvent:FireClient(plr, eggName, "Open3", petTable)
                    end)
                    return true
                end
            end
        end
    end
    return false
end

--// Buy Egg
function EggHandler:BuyEgg(plr, eggName, which)
    if plr and eggName ~= nil and which ~= nil then
        if self:CheckPetInv(plr, which) == true then
            local canOpenEgg = self:CheckIfCanOpenEgg(plr, tostring(eggName))
            if canOpenEgg == true then
                if self:CheckEggDistance(plr, tostring(eggName)) == true then
                    if which == "Buy1" then
                        local cost, currency = self:GetEggCostCurrency(tostring(eggName))
                        local stat = plr.leaderstats[currency].Value
                        if tonumber(stat) >= tonumber(cost) then
							plr.leaderstats[currency].Value = plr.leaderstats[currency].Value - tonumber(cost)
							plr.leaderstats.EggsOpened.Value = plr.leaderstats.EggsOpened.Value + 1
                            local pet = self:OpenEgg(plr, tostring(eggName), "OnePet")
                            if pet ~= nil then
                                self:AddTotalEggAmount(plr, 1)

                                local autoDeleted = self:CheckAutoDeletePet(plr, tostring(pet))

                                if autoDeleted == true then
                                    return {pet, false, false, true} 
                                else
                                    LegendaryPetChatModule:SendMessage(plr, "Pet", tostring(pet), tostring(eggName))

                                    local new = self:AddPetToIndex(plr, tostring(pet))
                                    local shiny = self:ReturnShinyChance(plr, tostring(eggName))
                                    if shiny == true then
                                        PetHandler:AddPet(plr, {PetName = tostring(pet), Evolution = "Shiny";})
                                        return {("Shiny ".. tostring(pet)), new, true, false}
                                    else
                                        PetHandler:AddPet(plr, {PetName = tostring(pet)})
                                        return {pet, new, false, false}
                                    end
                                end
                            end
                        else
                            local amountNeeded = tonumber(cost) - tonumber(stat)
                            return {"NeedMoney", tostring(currency), amountNeeded}
                        end
                    elseif which == "Buy3" then
                        if CheckPass(plr, "ThreeEggOpen", "Check") == true then
                            local cost,currency = self:GetEggCostCurrency(eggName)
							local stat = plr.leaderstats[currency].Value
                            local newCost = tonumber(cost) * 3
                            if tonumber(stat) >= tonumber(newCost) then
								plr.leaderstats[currency].Value = plr.leaderstats[currency].Value - newCost
								plr.leaderstats.EggsOpened.Value = plr.leaderstats.EggsOpened.Value + 3
                                local pet1, pet2, pet3 = self:OpenEgg(plr, eggName, "ThreePets")
                                if pet1 ~= nil and pet2 ~= nil and pet3 ~= nil then
                                    self:AddTotalEggAmount(plr, 3)

                                    local tbl1, tbl2, tbl3 = {}, {}, {}

                                    local autoDeleted1 = self:CheckAutoDeletePet(plr, tostring(pet1))
                                    local autoDeleted2 = self:CheckAutoDeletePet(plr, tostring(pet2))
                                    local autoDeleted3 = self:CheckAutoDeletePet(plr, tostring(pet3))

                                    if autoDeleted1 == true then
                                        tbl1 = {tostring(pet1), false, false, true}
                                    else
                                        LegendaryPetChatModule:SendMessage(plr, "Pet", tostring(pet1), tostring(eggName))
                                        local new1 = self:AddPetToIndex(plr, tostring(pet1))
                                        local shiny1 = self:ReturnShinyChance(plr, eggName)
                                        if shiny1 == true then
                                            PetHandler:AddPet(plr, {PetName = tostring(pet1), Evolution = "Shiny";})
                                            tbl1 = {("Shiny ".. tostring(pet1)), new1, true, false}
                                        else
                                            PetHandler:AddPet(plr, {PetName = tostring(pet1)})
                                            tbl1 = {tostring(pet1), new1, false, false}
                                        end
                                    end

                                    if autoDeleted2 == true then
                                        tbl2 = {tostring(pet2), false, false, true}
                                    else
                                        LegendaryPetChatModule:SendMessage(plr, "Pet", tostring(pet2), tostring(eggName))
                                        local new2 = self:AddPetToIndex(plr, tostring(pet2))
                                        local shiny2 = self:ReturnShinyChance(plr, eggName)
                                        if shiny2 == true then
                                            PetHandler:AddPet(plr, {PetName = tostring(pet2), Evolution = "Shiny";})
                                            tbl2 = {("Shiny ".. tostring(pet2)), new2, true, false}
                                        else
                                            PetHandler:AddPet(plr, {PetName = tostring(pet2)})
                                            tbl2 = {tostring(pet2), new2, false, false}
                                        end
                                    end

                                    if autoDeleted3 == true then
                                        tbl3 = {tostring(pet3), false, false, true}
                                    else
                                        LegendaryPetChatModule:SendMessage(plr, "Pet", tostring(pet3), tostring(eggName))
                                        local new3 = self:AddPetToIndex(plr, tostring(pet3))
                                        local shiny3 = self:ReturnShinyChance(plr, eggName)
                                        if shiny3 == true then
                                            PetHandler:AddPet(plr, {PetName = tostring(pet3), Evolution = "Shiny";})
                                            tbl3 = {("Shiny ".. tostring(pet3)), new3, true, false}
                                        else
                                            PetHandler:AddPet(plr, {PetName = tostring(pet3)})
                                            tbl3 = {tostring(pet3), new3, false, false}
                                        end
                                    end

                                    return tbl1, tbl2, tbl3
                                end
                            else
                                local amountNeeded = tonumber(newCost) - tonumber(stat)
                                return {"NeedMoney", tostring(currency), amountNeeded}
                            end
                        else
                            return "NeedPass"
                        end
                    end
                else
                    return "GetClose"
                end
            else
                return canOpenEgg
            end
        else
            return "Max"
        end
    end
end

function EggHandler:Int()
    ServerChatMessagesModule:Int()

    EggEvent.OnServerInvoke = function(...)
        return self:BuyEgg(...)
    end

    AutoDeleteEvent.OnServerInvoke = function(...)
        return AutoDeleteServerModule:ConfigAutoDelete(...)
    end
    GetAutoDeleteDataEvent.OnServerInvoke = function(...)
        return AutoDeleteServerModule:GetPlayerData(...)
    end
end

return EggHandler

Thank you for your time

First of all, make your own Enum for gamepass names, so the code becomes easier to read like:

local GamepassEnums = {
 ThreeEggOpen = 103483280;
 AutoHatch = 103112632;
}

Second thing, nested if statements are disgusting. Try to use guard if statements
more about it here

Third thing, if possible start using tables instead of if statements, it’s much easier to just read the code…

local objects = {
 ["case1"] = function()
   return true
 end,
 ["case2"] = function()
  return false
 end
}

objects["case1"]()
1 Like