function eggModule.hatch_egg(eggName, petName)
print(petName)
local eggMesh = eggs:FindFirstChild(eggName):FindFirstChild("Egg")
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
function eggModule.petViewport(frame, petName)
local pet = pets:FindFirstChild(petName)
if pet ~= nil then
local clonedPet = pet:Clone()
clonedPet.Parent = frame
clonedPet:SetPrimaryPartCFrame(CFrame.new(0,0,0))
local viewportCamera = Instance.new("Camera")
viewportCamera.Parent = frame
frame.CurrentCamera = viewportCamera
viewportCamera.CFrame = CFrame.new(0,0,clonedPet.PrimaryPart.Size.Z * 3)
pet_display_connection = runService.RenderStepped:Connect(function()
clonedPet:SetPrimaryPartCFrame(clonedPet.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(petRotationSpeed),0))
end)
wait(petDisplayDuration)
tweenService:Create(frame,TweenInfo.new(petFadeSpeed),{ImageTransparency = 1}):Play()
wait(petFadeSpeed * 1.05)
pet_display_connection:Disconnect()
viewportCamera:Destroy()
frame.Visible = false
frame.ImageTransparency = 0
viewportCamera:Destroy()
end
end
Module Script ^
local replicatedStorage = game:GetService("ReplicatedStorage")
local eggGUI = script.Parent:WaitForChild("EggGui")
local petDisplay = eggGUI:WaitForChild("PetDisplay")
local eggClient =require(replicatedStorage:WaitForChild("Egg_Module_Client"))
replicatedStorage.Remotes.ClientHatch.OnClientEvent:Connect(function(eggName,petName)
local hatch = eggClient.hatch_egg(eggName,petName)
petDisplay.Visible = true
eggClient.petViewport(petDisplay,petName)
end)
Local script ^
What is happening, is the first time i hatch an egg it works perfectly, but second time the first pet is in the background of the viewport as well as the new pet. Third time there are 2 pets in the background etc. Why is this happening?