Help with egg hatching system

I need help with an egg hatching system, everything else works except this. The egg doesn’t show up in front of the camera, despite the fact that the egg is a child of the camera. Please help.

Script:

function EggModule.Hatch_Egg(eggName, petName)
	local eggMesh = eggs[eggName]["Egg_Model"]
	if eggMesh ~= nil then
		local clonedMesh = eggMesh:Clone()
		clonedMesh.Parent = camera
		
		for _, obj in next, clonedMesh:GetDescendants() do
			if obj:IsA("BasePart") or obj:IsA("UnionOperation") then
				obj.CanCollide = false
				obj.CastShadow = false
			elseif obj:IsA("ProximityPrompt") then
				obj:Destroy()
			end
		end
		
		egg_Connection = runService.RenderStepped:Connect(function()
			local cf =  CFrame.new(0,0,clonedMesh.PrimaryPart.Size.Z*2)* CFrame.Angles(0,0,math.sin(time() * eggShakeSpeed)*eggShakeSize)
			clonedMesh:SetPrimaryPartCFrame(camera.CFrame * cf)
		end)
		wait(eggShakeDuration)
		for _, obj in next, clonedMesh:GetDescendants() do
			if obj:IsA("BasePart") or obj:IsA("UnionOperation") then
				tweenService:Create(obj, TweenInfo.new(eggFadeDuration),{Transparency = 1}):Play()
			end
		end
		wait(eggFadeDuration*1.08)
		egg_Connection:Disconnect()
		clonedMesh:Destroy()
		return petName
	end
end
3 Likes

Are there any errors in the output so I can take a look?

No errors at all (character limit)

Can you try adding print with a different letter from the alphabet in the print between each parts of code to see where it stops?

all of them printed, i think i messed up the CFrame values so it didn’t show on my camera

If you want to render something on the UI, use a Viewport Frame

can you please provide the eggShakeSpeed and eggShakeSize

I Got it working

local runService = game:GetService("RunService") 
local camera = game.Workspace.CurrentCamera
local clonedMesh = script.Parent.ViewportFrame:WaitForChild("Model")

local eggShakeSpeed = 5 -- random stuff
local eggShakeSize = 2-- random stuff

egg_Connection = runService.RenderStepped:Connect(function()
	local cf =  CFrame.new(0,0,-7)* CFrame.Angles(0,0,math.sin(time() * eggShakeSpeed)*eggShakeSize)
	clonedMesh:SetPrimaryPartCFrame(cf)
end)

CFrame.new(0,0,-7) this puts the part bit far from the camera

heres a .rbxm file to test it out
stuff.rbxm (5.7 KB)

edit: if this solves the problem mark this as solved

Thanks! (character limit) (more character limit)

1 Like