I am following a tutorial to create a pet hatching system but I am having a issue with spawning the pet to show it “coming out of the egg”. I have managed to find the problem, but I cannot find the solution.
The code which spawns this copy of the pet to “hatch”:

The problem is the line of code, which is:
‘’’ local pet = pets:FindFirstChild(petName)
‘’’
The pets variable is:

I used a print statement to see what pet was holding, but it came back ‘nil’. But which I took the printName variable which is sent to the function from the EggClietSystem and I parented it to the player it showed the model for the selected pet. I also run a check in the petViewport() function which came back with the variable being an instance of a ‘model’.

This is the path to the pets folder that I am trying to refence, to be able to find the matching pet and add to the script, but I cannot find the solution to this problem.
Can you show us what’s calling the eggModuleClient.PetViewport() function? It seems like the issue is the petName parameter which is already nil when the function is called.
The petName is not nill, it always returns the name of the pet, but can’t get the object from replicated storage. The code for this is:

I added the petName.Parent as a test to show you that the variable refences the model of a pet, as seen in the screenshot below:

Why is petName a model and not a string? Makes no sense, and is also the cause of your problem. The :FindFirstChild() method accepts a string as an argument. This means you need to replace pets:FindFirstChild(petName) with pets:FindFirstChild(petName.Name).
And a suggestion, you can remove ~= nil. The if statement will work either way, since a value that just exists will return true in Lua.
1 Like
The output is still showing nill for petName.name:
‘’’
local pet = pets:FindFirstChild(petName.Name)
‘’’
Edit:
It does work, I just forgot to remove the check I put in place to check for the model. After I removed that, the code worked.