Problem with egg hatching

Hi devs, i wanted to do an hatching animation for my eggs.

The problem is, when i try to hatch a pet, the script fires the remote to the client with the player, the pet, and the egg name(or id) the output says: Argument 1 missing or nil

I tried to print the “EggsConfig[eggId]” and it prints nil.

These are the scripts:
(Don’t mind the marketplace thing)

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local MarketPlaceService = game:GetService("MarketplaceService")

local EggsFolder = workspace.Eggs

local Rewards = require(ServerScriptService.Utils.Rewards)
local EggsConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("EggsConfig"))
local BuyEgg = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("BuyEgg")
local PetHatched = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("PetHatched")
local EggAnimation = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("EggAnimation")

local devProductID = "1502024746"

local HATCH_COOLDOWN = 3

local HatchCooldown = {}

local function onProductPurchaseFinished(player, productId, receipt)
	if productId == devProductID then
		local success, message = pcall(function()
			local marketplaceService = game:GetService("MarketplaceService")
			local info = marketplaceService:GetProductInfo(productId, Enum.InfoType.Product)
		end)
		
		if not success then
			warn("Error processing receipt: " .. message)
		end
	end
end

local function promptPurchase(player, productId, receipt)
	MarketPlaceService:PromptProductPurchase(player, productId)
	
	if MarketPlaceService.PromptProductPurchaseFinished then
		onProductPurchaseFinished()
		else return
	end
end

local function ChoosePet(pets: table)
	local totalWeight = 0
	
	for _, pet in ipairs(pets) do
		totalWeight += pet.Chance
	end
	
	local chance = math.random(0, totalWeight)
	local counter = 0
	for _, pet in ipairs(pets) do
		counter += pet.Chance
		if chance <= counter then
			return pet
		end
	end
end

local function ID(eggConfig)
	local id 
	
	if eggConfig.ID == 1 then
		id = 1
	elseif eggConfig.ID == 2 then
		id = 2
	elseif eggConfig.ID == 3 then
		id = 3
	end
	
	return id
end

local function Hatch(player: Player, eggId: string)
	if HatchCooldown[player.UserId] then return end
	
	local eggConfig = EggsConfig[eggId]
	local playerBalance = player.leaderstats.Coins.Value
	
	if EggsConfig[eggId].Robux == true then
		promptPurchase(player, devProductID)
		
		local pet = ChoosePet(eggConfig.Pets)
		local petInstance = Rewards.Pet(player, pet.ID, pet.Rarity)
		
		PetHatched:FireClient(player, pet, petInstance)
		EggAnimation:FireClient(player, pet, ID(EggsConfig))

		task.delay(HATCH_COOLDOWN, function()
			HatchCooldown[player.UserId] = nil
		end)
	else 
		if playerBalance < eggConfig.Price then return end
		HatchCooldown[player.UserId] = true

		player.leaderstats.Coins.Value -= eggConfig.Price
	
		local pet = ChoosePet(eggConfig.Pets)
		local petInstance = Rewards.Pet(player, pet.ID, pet.Rarity)
		
		PetHatched:FireClient(player, pet, petInstance)
		EggAnimation:FireClient(player, pet, ID(eggConfig))
	
		task.delay(HATCH_COOLDOWN, function()
			HatchCooldown[player.UserId] = nil	
		end)
	end
end

local function CanHatch(player: Player, eggId: string)
	local eggConfig = EggsConfig[eggId]
	
	if not eggConfig then return false end
	
	local playerBalance = player.leaderstats.Coins.Value
	return playerBalance >= eggConfig.Price
end

BuyEgg.OnServerEvent:Connect(function(player: Player, eggId: string, action: string)
	if not CanHatch(player, eggId) then return end
	
	if action == "Auto" then
		--
	elseif action == "x3" then
		--
	else
		Hatch(player, eggId)
	end
end)

Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Lighting = game:GetService("Lighting")

local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local EggAnimation = Remotes:WaitForChild("EggAnimation")

local EggView = script.Parent.EggAnimation.Main.Egg
local PetView = script.Parent.EggAnimation.Main.Pet

local GUI_BLACKLIST = {"Shop", "Eggs", "Scripts"}

local function ID(id)
	local name
	
	if id == 1 then
		name = "Basketball_Egg"
	elseif id == 2 then
		name = "Rare_Basketball_Egg"
	elseif id == 3 then
		name = "Premium_Egg"
	end
	
	return name
end

EggView.Visible = false
PetView.Visible = false

EggAnimation.OnClientEvent:Connect(function(player, pet, eggConfig)
	local petModel = ReplicatedStorage.Pets:FindFirstChild(pet)
	local eggModel = ReplicatedStorage.EggsModel:FindFirstChild(ID(eggConfig))

	script.Parent.EggAnimation.Enabled = true
	Lighting.Blur.Enabled = true
	
	local cloneEgg = eggModel:Clone()
	cloneEgg.Parent = EggView
	EggView.Visible = true
	
	local camera = Instance.new("Camera", EggView)
	EggView.CurrentCamera = camera
	camera.CFrame = CFrame.new(Vector3.new(-1,1,4), eggModel.PrimaryPart.Position)
	camera.FieldOfView = 7
	
	cloneEgg.PrimaryPart.Orientation = Vector3.new(0,0,-70)
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, 1, true)
	local tween = TweenService:Create(cloneEgg.PrimaryPart, tweenInfo, {Orientation = Vector3.new(0,0,70)})
	
	tween:Play()
	
	for _, gui in ipairs(script.Parent.Parent.Eggs:GetChildren()) do
		if gui:IsA("LocalScript") or gui:IsA("ScreenGui") or gui:IsA("TextButton") then continue end
		gui.Enabled = true
	end
	
	local enabledGuis = {}
	for _, gui in ipairs(script.Parent.Parent:GetChildren()) do
		if gui:IsA("Folder") and table.find(GUI_BLACKLIST, gui.Name) then continue end
		if gui.Enabled == true then
			table.insert(enabledGuis, gui)
			gui.Enabled = false
		end
	end
	
	tween.Completed:Connect(function()
		for _, children in ipairs(EggView:GetChildren()) do
			children:Destroy()
		end
		
		EggView.Visible = false
		PetView.Visible = true
		
		task.delay(3, function()
			PetView.Visible = false
			EggView.Visible = false
			script.Parent.EggAnimation.Enabled = false
			Lighting.Blur.Enabled = false
			
			for _, gui in enabledGuis do
				gui.Enabled = true
			end
			
			for _, gui in ipairs(script.Parent.Parent.Eggs:GetChildren()) do
				if gui:IsA("LocalScript") or gui:IsA("ScreenGui") or gui:IsA("TextButton") then continue end
				gui.Enabled = true
			end
		end)
	end)
end)

Thanks!

Is it a number or string? What is it in your “EggsConfig”?

I printed eggId, this is the module script “EggsConfig”:

local module = {
	Basketball_Egg = {
		Price = 2_000,
		Robux = false,
		ID = 1,
		Pets = {
			{
				ID = "Dog", Rarity = "Common", Chance = 30
			},
			{
				ID = "Cat", Rarity = "Common", Chance = 25
			},
			{
				ID = "Seal", Rarity = "Uncommon", Chance = 20
			},
			{
				ID = "Doge", Rarity = "Uncommon", Chance = 15
			},
			{
				ID = "Chicken", Rarity = "Rare", Chance = 10
			},
			{
				ID = "Giant_Doge", Rarity = "Secret", Chance = 0.001
			},
			{
				ID = "Giant_Chicken", Rarity = "Secret", Chance = 0.001
			},
		}
	},

	Rare_Basketball_Egg = {
		Price = 5_000,
		Robux = false,
		ID = 2,
		Pets = {
			{
				ID = "Pig", Rarity = "Uncommon", Chance = 50
			},
			{
				ID = "Dog", Rarity = "Uncommon", Chance = 50
			},
		}
	},
	
	Premium_Egg = {
		Price = 199,
		Robux = true,
		ID = 3,
		Pets = {
			{
				ID = "Neon_Agony", Rarity = "Premium", Chance = 70
			},
			{
				ID = "Neon_Angel", Rarity = "Premium", Chance = 30
			},
		}
	}
}

return module

image

Try putting quotes around the ID because here it’s a string:

Sorry, i don’t understand english too much, but instead of using the id function i put eggId because i already have the name(eggId), but same error.

like this:

EggAnimation:FireClient(player, pet, eggId)

Output:

Argument 1 missing or nil

That’s because the player does not exist in the scope of the function.

1 Like

I removed the player in

EggAnimation.OnClientEvent:Connect(function(pet, eggId)

Now it works! Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.