Is there any way to fix sound delay and animation for my egg system?

hi,
i made egg hatching system but when player hatching egg the sound with delay and if you rotate the camera is broke the animation sometimes, is there any way to fix this problems?
here is the game:
https://www.roblox.com/games/5863375772/Game-Test

and script:

--local Eggs = game.Workspace.Eggs:GetChildren()
local UserInputService = game:GetService("UserInputService")
local EggName = "None"
local CanHatch = true
local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local Rps = game:GetService("ReplicatedStorage")
local cam = workspace.Camera
local Ebtn = script.Parent.PercentUI
local TweenService = game:GetService("TweenService")
local Skipped = false
local Tweeninfo = TweenInfo.new(.07, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 5, true, 0.05)
local PetSelected = "None"
--[[local Min = 1
local Max = 5]]--
local EggsModule = require(Rps.Eggs)
local Counter = 1
local PetName = "None"
local MaxEquip = 1
local MaxStorage = 100
local PetsCount = 0
local PetsEquipCount = 0
local PetStatsModule = require(Rps.Eggs.PetStats)

function DragPet(Pet)
	Counter = 1
	repeat wait(0.05)
		Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame - Vector3.new(0,-.09,0)
		Counter = Counter + 1
	until Counter == 150
end

script.Parent.Inventory.CloseBtn.MouseButton1Click:Connect(function()
	script.Parent.Inventory.Visible = not script.Parent.Inventory.Visible
end)

script.Parent.InvBtn.MouseButton1Click:Connect(function()
	script.Parent.Inventory.Visible = not script.Parent.Inventory.Visible
end)

local template = script:WaitForChild("Template")

local ButtonConnections = {}

local function setTemplateEquipped(template)
	for _,v in pairs(script.Parent.Inventory.Background.Items:GetChildren()) do
		if v:FindFirstChild("Equiped") then
			v.Equiped.Text = "Unequip"
		end
	end
	
	template.Equiped.Text = "Equiped"
end

local function addPet(pet)
	local NewPetTemplate = template:Clone()
	NewPetTemplate.Name = pet.Name
	NewPetTemplate.PetName.Text = pet.Name
	NewPetTemplate.Parent = script.Parent.Inventory.Background.Items
	
	local NewPet = pet:Clone()
	NewPet.Parent = NewPetTemplate.ViewportFrame
	
	local camera = Instance.new("Camera")
	camera.CFrame = CFrame.new(NewPet.PrimaryPart.Position + (NewPet.PrimaryPart.CFrame.lookVector * 4),NewPet.PrimaryPart.Position)
	camera.Parent = NewPetTemplate.ViewportFrame
	
	NewPetTemplate.ViewportFrame.CurrentCamera = camera
	
	ButtonConnections[#ButtonConnections+1] = NewPetTemplate.MouseButton1Click:Connect(function()
		PetName = NewPetTemplate.Name
		print(PetName)
		script.Parent.Inventory.PetInfo.PetName.Text = PetName
		if script.Parent.Inventory.PetInfo.EquipBtn.Text == "Equiped" then
			game.ReplicatedStorage.RemoteEvents.UnequipPet:FireServer()
		else
			game.ReplicatedStorage.RemoteEvents.EquipPet:FireServer(PetName)
		end
		if NewPetTemplate.Equiped.Text == "Equiped" then
			--Unequip Function
			game.ReplicatedStorage.RemoteEvents.UnequipPet:FireServer()
			NewPetTemplate.Equiped.Text = "Unequip"
		else
			--Equip Function
			game.ReplicatedStorage.RemoteEvents.EquipPet:FireServer(PetName)
			setTemplateEquipped(NewPetTemplate)
		end
		script.Parent.Inventory.PetInfo.CoinsMulti.Text = "x".. PetStatsModule[PetName].Coins .." Coins"
		script.Parent.Inventory.PetInfo.GemMulti.Text = "x".. PetStatsModule[PetName].Gems .." Gems"
	end)
	
end

script.Parent.HatchFrame.SkipBtn.MouseButton1Click:Connect(function()
	if cam:FindFirstChildWhichIsA("Model") then
		cam:FindFirstChildWhichIsA("Model"):Destroy()
	end
	CanHatch = true
	print(CanHatch)
	script.Parent.HatchFrame.Visible = false
	Skipped = true
	wait(3.2)
	Skipped = false
	for _,v in pairs(player.PlayerGui:GetChildren()) do
		v.Enabled = true
	end
	script.Parent.InvBtn.Visible = true
end)

spawn(function()
	while wait(0.005) do
		Ebtn.Visible = false
		for _,v in pairs(workspace:WaitForChild("PercentInfo"):GetChildren()) do
			if (v.Position-player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude <= 8 and CanHatch then
				Ebtn.Visible = true
				EggName = v.Name
				Ebtn.PriceT.Text = EggsModule[EggName].Price
				Ebtn.NameT.Text = EggName
			--[[	if EggsModule[EggName].Pets[1] then
					Ebtn.PetsFrame.Pet1.Visible = true
					local Camera = Instance.new("Camera")
					Camera.Parent = Ebtn.PetsFrame.Pet1
					Ebtn.PetsFrame.Pet1.CurrentCamera = Camera
				end
				if EggsModule[EggName].Pets[2] then
					Ebtn.PetsFrame.Pet2.Visible = true
				end
				if EggsModule[EggName].Pets[3] then
					Ebtn.PetsFrame.Pet3.Visible = true
				end
				if EggsModule[EggName].Pets[4] then
					Ebtn.PetsFrame.Pet4.Visible = true
				end
				if EggsModule[EggName].Pets[5] then
					Ebtn.PetsFrame.Pet5.Visible = true
				end
				if EggsModule[EggName].Pets[6] then
					Ebtn.PetsFrame.Pet6.Visible = true
				end
				if EggsModule[EggName].Pets[7] then
					Ebtn.PetsFrame.Pet7.Visible = true
				end
				if EggsModule[EggName].Pets[8] then
					Ebtn.PetsFrame.Pet8.Visible = true
				end]]--
				--print(v.Name)
			end
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if cam:FindFirstChildWhichIsA("Model") then
		cam:FindFirstChildWhichIsA("Model"):SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,0,-7))
	end
end)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E and Ebtn.Visible == true and CanHatch then
		if PetsCount <= MaxStorage and PetsCount ~= MaxStorage and player.leaderstats.Coins.Value >= EggsModule[EggName].Price then
			for _,v in pairs(player.PlayerGui:GetChildren()) do
				v.Enabled = false
			end
			script.Parent.Enabled = true
			script.Parent.Inventory.Visible = false
			script.Parent.InvBtn.Visible = false
			game.ReplicatedStorage.RemoteEvents.HatchEgg:FireServer(EggsModule[EggName].Price)
			CanHatch = false
			script.Parent.HatchFrame.SkipBtn.Visible = false
			script.Parent.HatchFrame.Visible = false
			print(CanHatch)
			print(EggName)
			local EggClone = Rps.EggsFolder:FindFirstChild(EggName):Clone()
			EggClone.Parent = cam
			local Goals = {
				Orientation = Vector3.new(EggClone.Egg.Orientation.X, EggClone.Egg.Orientation.Y, 25.03);
			};
			local Goals2 = {
				Orientation = Vector3.new(EggClone.Egg.Orientation.X, EggClone.Egg.Orientation.Y, -25.03);
			};
			local Tween1 = TweenService:Create(EggClone.Egg, Tweeninfo, Goals2)
			wait(1)
			Tween1:Play()
			wait(2)
			local Tween2 = TweenService:Create(EggClone.Egg, Tweeninfo, Goals)
			Tween2:Play()
			script.Parent.Sounds.Pop_Soft:Play()
			wait(script.Parent.Sounds.Pop_Soft.TimeLength)
			EggClone.Egg.Orientation = Vector3.new(0,0,25)
			script.Parent.Sounds.Pop_Soft:Play()
			wait(script.Parent.Sounds.Pop_Soft.TimeLength)
			EggClone.Egg.Orientation = Vector3.new(0,0,-25)
			script.Parent.Sounds.Pop_Soft:Play()
			wait(script.Parent.Sounds.Pop_Soft.TimeLength)
			EggClone.Egg.Orientation = Vector3.new(0,0,25)
			script.Parent.Sounds.Pop_Soft:Play()
			wait(script.Parent.Sounds.Pop_Soft.TimeLength)
			EggClone.Egg.Orientation = Vector3.new(0,0,-25)
			script.Parent.Sounds.Pop_Soft:Play()
			wait(script.Parent.Sounds.Pop_Soft.TimeLength)
			EggClone.Egg.Orientation = Vector3.new(0,0,25)
			script.Parent.Sounds.Pop_Soft:Play()
			wait(script.Parent.Sounds.Pop_Soft.TimeLength)
			EggClone.Egg.Orientation = Vector3.new(0,0,-25)
			script.Parent.Sounds.Pop_Soft:Play()
			wait(script.Parent.Sounds.Pop_Soft.TimeLength)
			EggClone.Egg.Orientation = Vector3.new(0,0,25)
			script.Parent.Sounds.Pop_Soft:Play()
			wait(script.Parent.Sounds.Pop_Soft.TimeLength)
			EggClone.Egg.Orientation = Vector3.new(0,0,-25)
			script.Parent.Sounds.Pop_Soft:Play()
			wait(0 + 0.5)
			if cam:FindFirstChildWhichIsA("Model") then
				cam:FindFirstChildWhichIsA("Model"):Destroy()
			end
			wait(0.005)
			PetSelected = EggsModule[EggName].Pets[math.random(EggsModule[EggName].Min,EggsModule[EggName].Max)].Name
			game.ReplicatedStorage.RemoteEvents.HatchPet:FireServer(PetSelected)
			print(PetSelected)
			addPet(game.ReplicatedStorage.PetsFolder:FindFirstChild(PetSelected))
			local PetClone = Rps.PetsFolder:FindFirstChild(PetSelected):Clone()
			PetClone.Parent = cam
			PetClone.PrimaryPart.CFrame = CFrame.new(PetClone.PrimaryPart.Position + (PetClone.PrimaryPart.CFrame.lookVector * 4),PetClone.PrimaryPart.Position)
			if PetClone:FindFirstChild("PetName") then
				PetClone:FindFirstChild("PetName"):Destroy()
			end
			local EffectClone = script.Part:Clone()
			EffectClone.Parent = PetClone
			EffectClone.CFrame = PetClone.PrimaryPart.CFrame
			script.Parent.HatchFrame.PetName.Text = PetSelected
			script.Parent.HatchFrame.Visible = true
			wait(0.7)
			--	DragPet(PetClone)
			script.Parent.HatchFrame.SkipBtn.Visible = true
			if not Skipped then
				wait(3)
				if cam:FindFirstChildWhichIsA("Model") then
					cam:FindFirstChildWhichIsA("Model"):Destroy()
				end
				CanHatch = true
				print(CanHatch)
				script.Parent.HatchFrame.Visible = false
				for _,v in pairs(player.PlayerGui:GetChildren()) do
					v.Enabled = true
				end
				script.Parent.InvBtn.Visible = true
			end
		end
	end
end)

--[[while wait() do
	wait(5)
	addPet(game.ReplicatedStorage.PetsFolder["oof Pet"])
end]]--

game.ReplicatedStorage.RemoteEvents.SendData.OnClientEvent:Connect(function(data)
	warn("Pet Inventory Loaded.")
	for _,v in pairs(data) do
		if game.ReplicatedStorage.PetsFolder:FindFirstChild(v) then
			addPet(game.ReplicatedStorage.PetsFolder:FindFirstChild(v))
		end
	end
end)

game.ReplicatedStorage.RemoteEvents.SetEquiped.OnClientEvent:Connect(function(EquipedPetName)
	if script.Parent.Inventory.Background.Items:FindFirstChild(EquipedPetName) then
		setTemplateEquipped(script.Parent.Inventory.Background.Items[EquipedPetName])
	end
end)

wait(2)
for _,v in pairs(game.Players.LocalPlayer.PetInventory:GetChildren()) do
	PetsCount = PetsCount + 1
end
game.Players.LocalPlayer.PetInventory.ChildAdded:Connect(function()
	PetsCount = PetsCount + 1
end)
game.Players.LocalPlayer.PetInventory.ChildRemoved:Connect(function()
	PetsCount = PetsCount - 1
end)

script.Parent.Inventory.PetInfo.DeleteBtn.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RemoteEvents.DeletePet:FireServer(PetName, false)
end)

script.Parent.Inventory.DeleteAllBtn.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RemoteEvents.DeletePet:FireServer(PetName, true)
end)

while wait() do
	if player.EquipedPet.Value ~= "" then
		PetsEquipCount = 1
	elseif player.EquipedPet.Value == "" then
		PetsEquipCount = 0
	end
	if PetsCount == MaxStorage or MaxStorage == PetsCount then
		script.Parent.Inventory.SlotsT.TextColor3 = Color3.fromRGB(255, 80, 80)
	else
		script.Parent.Inventory.SlotsT.TextColor3 = Color3.fromRGB(255, 255, 255)
	end
	if PetsEquipCount == MaxEquip or MaxEquip == PetsEquipCount then
		script.Parent.Inventory.EquipT.TextColor3 = Color3.fromRGB(255, 80, 80)
	else
		script.Parent.Inventory.EquipT.TextColor3 = Color3.fromRGB(255, 255, 255)
	end
	script.Parent.Inventory.SlotsT.Text = PetsCount .."/".. MaxStorage
	script.Parent.Inventory.EquipT.Text = PetsEquipCount .."/".. MaxEquip
	if script.Parent.Inventory.FindPet.Text ~= "" then
		for _,v in pairs(script.Parent.Inventory.Background.Items:GetChildren()) do
			if v:IsA("ImageButton") then
				if v.Name == script.Parent.Inventory.FindPet.Text then
					v.Visible = true
				else
					v.Visible = false
				end
			end
		end
	else
		for _,v in pairs(script.Parent.Inventory.Background.Items:GetChildren()) do
			if v:IsA("ImageButton") then
				v.Visible = true
			end
		end
	end
end

there is no error or anything on the developer console.

1 Like

Your script is way too long. Can you provide full information on what you are trying to do here and i know it’s about the egg system

i’m trying to fix problem when you hatch egg and then rotate the camera so is glitch the animation of the egg, also when you hatch egg is waiting around 3 seconds before is play the sounds of the hatching.

Ah i get it that’s because the sound plays as soon the egg finishes the tween