local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local displayEggEvent = ReplicatedStorage:WaitForChild("DisplayEggEvent")
local displayPetEvent = ReplicatedStorage:FindFirstChild("DisplayPetEvent")

-- Function to create the egg tilting animation
local function tiltEgg(egg)
	local originalCFrame = egg.CFrame
	local tiltTime = 0.15
	local tiltAngle = math.rad(15)
	for i = 1, 5 do
		local leftTilt = TweenService:Create(egg, TweenInfo.new(tiltTime / 2), { CFrame = originalCFrame * CFrame.Angles(0, 0, tiltAngle) })
		local rightTilt = TweenService:Create(egg, TweenInfo.new(tiltTime / 2), { CFrame = originalCFrame * CFrame.Angles(0, 0, -tiltAngle) })
		leftTilt:Play()
		leftTilt.Completed:Wait()
		rightTilt:Play()
		rightTilt.Completed:Wait()
	end
	local finalTween = TweenService:Create(egg, TweenInfo.new(tiltTime / 2), { CFrame = originalCFrame })
	finalTween:Play()
	finalTween.Completed:Wait()
end

-- Define the pet/egg rarities with colors
local itemRarities = {
	["Dog"] = {rarity = "Common", color = Color3.new(1, 1, 1)}, -- White
	["Bird"] = {rarity = "Common", color = Color3.new(1, 1, 1)}, -- White
	["Mega Dog"] = {rarity = "Rare", color = Color3.new(0, 0, 1)}, -- Blue
	["Golden Bird"] = {rarity = "Epic", color = Color3.new(1, 0.5, 0)}, -- Orange
	["Golden Dog"] = {rarity = "Epic", color = Color3.new(0.666667, 0, 1)}, -- Orange
	["Superior Dog"] = {rarity = "Legendary", color = Color3.new(1, 0.84, 0)}, -- Gold
	["InfernoDog"] = {rarity = "Mythical", color = Color3.new(0.5, 0, 0.5)}, -- Purple
	["DiamondDog"] = {rarity = "Mythical", color = Color3.new(0.5, 0, 0.5)}, -- Purple
	["Flare Dog"] = {rarity = "Mythical", color = Color3.new(0.5, 0, 0.5)}, -- Purple
	["blupart"] = {rarity = "Admin", color = Color3.new(1, 0, 0)}, -- Red
	["mp_51a"] = {rarity = "Admin", color = Color3.new(1, 0, 0)}, -- Red
	["Alpha Wolf"] = {rarity = "Exclusive", color = Color3.new(0.8, 0.6, 1)}, -- Light violet
	["Fire Reaper"] = {rarity = "Exclusive", color = Color3.new(0.8, 0.6, 1)}, -- Light violet
}

local function displayItemInViewport(itemType, isEgg)
	print("Attempting to display " .. (isEgg and "egg" or "pet") .. " for player: " .. player.Name)
	local eggSequence = playerGui:WaitForChild("EggSequence")
	local viewportFrame = eggSequence:WaitForChild("ViewportFrame")
	local rarityLabel = eggSequence:WaitForChild("RarityLabel")
	local petNameLabel = eggSequence:WaitForChild("PetName")

	-- Clear existing content in the ViewportFrame
	for _, child in pairs(viewportFrame:GetChildren()) do
		if child:IsA("Model") or child:IsA("Part") then
			child:Destroy()
		end
	end

	-- Clone the item and add it to the ViewportFrame
	local itemModel
	if isEgg then
		itemModel = ReplicatedStorage.Eggs:FindFirstChild(itemType)
	else
		itemModel = ReplicatedStorage.Pet:FindFirstChild(itemType)
	end

	if not itemModel then
		warn((isEgg and "Egg" or "Pet") .. " model not found for type: " .. itemType)
		return
	end

	local itemClone = itemModel:Clone()
	itemClone.Parent = viewportFrame

	-- Set up the camera
	local camera = viewportFrame:FindFirstChildOfClass("Camera")
	if not camera then
		camera = Instance.new("Camera")
		viewportFrame.CurrentCamera = camera
		camera.Parent = viewportFrame
	end

	-- Position the item and camera
	if isEgg then
		itemClone:PivotTo(CFrame.new(0, 0, 0))
		camera.CFrame = CFrame.new(Vector3.new(0, 0, 5), Vector3.new(0, 0, 0))
	else
		itemClone:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))
		local itemSize = itemClone:GetExtentsSize()
		local cameraDistance = math.max(itemSize.X, itemSize.Y, itemSize.Z) * 2
		camera.CFrame = CFrame.new(Vector3.new(0, itemSize.Y / 2, cameraDistance), Vector3.new(0, itemSize.Y / 2, 0))
	end

	-- Update the rarity label and pet name
	local itemInfo = itemRarities[itemType]
	if itemInfo then
		rarityLabel.Text = itemInfo.rarity
		rarityLabel.TextColor3 = itemInfo.color
		rarityLabel.Visible = true

		petNameLabel.Text = itemType
		petNameLabel.Visible = true
	else
		rarityLabel.Visible = false
		petNameLabel.Visible = false
	end

	print((isEgg and "Egg" or "Pet") .. " displayed successfully for player: " .. player.Name)

	if isEgg then
		-- Tilt the egg
		tiltEgg(itemClone)

		-- After tilting, remove the egg
		itemClone:Destroy()

		-- We don't need to trigger pet display here since it's handled in onPromptActivated
	else
		-- Function to spin the pet
		local spinConnection
		spinConnection = game:GetService("RunService").RenderStepped:Connect(function()
			if itemClone.Parent then
				itemClone:SetPrimaryPartCFrame(itemClone.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(1), 0))
			else
				spinConnection:Disconnect()
			end
		end)

		-- Remove the pet after 2 seconds
		task.delay(2, function()
			itemClone:Destroy()
			print("Pet removed for player: " .. player.Name)
			rarityLabel.Visible = false -- Hide the rarity label when the pet is removed
			petNameLabel.Visible = false -- Hide the pet name label when the pet is removed
		end)
	end
end

displayEggEvent.OnClientEvent:Connect(function(eggType)
	displayItemInViewport(eggType, true)
end)

displayPetEvent.OnClientEvent:Connect(function(petType)
	displayItemInViewport(petType, false)
end)

print("Local script setup completed successfully") (instead of just cloning the egg and tilt. heres what you need to modify. each tilt is 3 times. so. first tilt to the left. it's 0.35 each tilt. then right. and then left tilt before stopping the tilt and revealing what pet did the player get.)