Table Filled, Still Returns Nil?

This pet hatching script I am using will return nil occasionally. I have literally no idea why this happens, and all of the print statements come out right, except it will just (seemingly at random) return nil from time to time. It is important to note that, when I use a predefined table instead of the method I am currently using, it does not return nil. My only guess would be to use a wait system, but I can’t even figure that out. Anyone have any ideas?

Module:

local config = {}

config.Price = 25

local folder = game.ReplicatedStorage:WaitForChild("PetResults"):WaitForChild("Basic Egg") -- Get the Basic Egg folder from ReplicatedStorage
local foldtable = folder:GetChildren()

config.HatchablePets = {} -- Initialize an empty table

-- Iterate over each folder inside foldtable
for i = 1, #foldtable do
	local subFolder = foldtable[i]
	local subFolderChildren = subFolder:GetChildren()

	-- Iterate over the children of the current subFolder and add their names to the HatchablePets table
	for j = 1, #subFolderChildren do
		local child = subFolderChildren[j]
		if child:IsA("Model") and child.Name then
			print("Added "..child.Name.." to Table!")
			table.insert(config.HatchablePets, tostring(child.Name))
		end
	end
end

print(config.HatchablePets)

config.Chances = {
	Common = 70,
	Uncommon = 25,
	Rare = 4.5
}

return config

Server Script:

-- This is a cut down version of my original code
remotes:WaitForChild("HatchPet").OnServerEvent:Connect(function(plr, incubator)
	if plr and incubator and incubator.Parent == workspace.Incubators then

		local incubatorConfig = require(incubator.Configuration)

		if plr.leaderstats.Coins.Value >= incubatorConfig.Price and #plr.PetsInventory:GetChildren() < config.MaxPetsInventory then
			plr.leaderstats.Coins.Value -= incubatorConfig.Price

			local chances = incubatorConfig.Chances
			local plrChance = rnd:NextNumber() * 100

			local n = 0
			local rarityChosen = nil

			for rarity, chance in pairs(chances) do 
				n += chance
				if plrChance <= n then
					rarityChosen = rarity
					break
				end
			end

			local hatchablePets = incubatorConfig.HatchablePets
			
			print(hatchablePets)

			for i = #hatchablePets, 2, -1 do
				local j = rnd:NextInteger(1, i)
				hatchablePets[i], hatchablePets[j] = hatchablePets[j], hatchablePets[i]
			end
			
			local petChosen
						
			for _, petName in pairs(hatchablePets) do
				if pets:FindFirstChild(petName, true) and pets:FindFirstChild(petName, true).Parent.Name == rarityChosen and pets:FindFirstChild(petName, true) ~= nil then
					petChosen = petName
					break
				end
			end
			
			if petChosen ~= nil then
				add(plr, petChosen)
			end
			
			print(petChosen)

			remotes:WaitForChild("HatchPet"):FireClient(plr, petChosen)
		end		
	end
end)
1 Like

Forgot to mention this…
The error it returns is Argument 1 missing or nil in a client script which uses this code:

remotes:WaitForChild("HatchPet").OnClientEvent:Connect(function(petName)
	
	print(petName)
	
	local foundPet = pets:FindFirstChild(petName, true)
	
	if foundPet and foundPet ~= nil then
		setupPetFrame(hatchedFrame:WaitForChild("PetFrame"), foundPet)
		
		incubatorFrame.Visible = false
		incubatorGui.Enabled = false
		hatchedFrame.Visible = true
		hatchedGui.Enabled = true
	end
end)

The error comes from the local foundPet = pets:FindFirstChild(petName, true) line.

2 Likes

Can you provide the add function after checking that the chosen pet is not nil? It’s most likely from that.

2 Likes

Sure, here it is;

local rs = game:GetService("ReplicatedStorage")
local pets = rs:WaitForChild("Pets")
local config = require(rs:WaitForChild("CONFIGURATION"))


function addFunc(plr:Player, petName:string)

	if plr and #plr.PetsInventory:GetChildren() < config.MaxPetsInventory then
		local foundPet = pets:FindFirstChild(petName, true)
		if foundPet then
			local newValue = Instance.new("ObjectValue")
			newValue.Value = foundPet
			newValue.Parent = plr.PetsInventory
		end
	end
end

return addFunc
1 Like

If that’s a module with a single function, you can pass it to this:

local rs = game:GetService("ReplicatedStorage")
local pets = rs:WaitForChild("Pets")
local config = require(rs:WaitForChild("CONFIGURATION"))

return function(plr: Player, petName: string)
	if plr and #plr.PetsInventory:GetChildren() < config.MaxPetsInventory then
		local foundPet = pets:FindFirstChild(petName, true)
		if foundPet then
			local newValue = Instance.new("ObjectValue")
			newValue.Value = foundPet
			newValue.Parent = plr.PetsInventory
		end
	end
end
1 Like

I’m not sure I understand, can you elaborate?

1 Like

Can you confirm that an ObjectValue is created and has its value set when this function is ran?

1 Like

I think you are doing something like this:

local add = require(path.to.add) 

if petChosen ~= nil then
	add(plr, petChosen)
end

For that to work, it must return THE FUNCTION, and not a table containing it.
If the correction doesn’t work for you, then the problem must be something else.

1 Like

Yes, it is created. I will add some print statements just to be sure.

Ah, I see what you mean. This does not work unfortunately.
Just to make sure I am understanding correctly, I changed the module to the code you provided. The code works fine, but it still returns nil from time to time.

All solutions tried have not worked, anyone have other ideas?