Trading Pets Bug

Server Side
local function OnFollow(player,petType,egg,uniquePetID)
	print("OnFollw")
	--print(petType,player,egg,uniquePetID,"What here")
	if not playerPets[player.UserId]  then
		playerPets[player.UserId] = {}
	end 

	if not equippedPetsCount[player.UserId] then
		equippedPetsCount[player.UserId] = 0
	end
	if  equippedPetsCount[player.UserId] >= MaxPetEqquiped then
		warn("Player has already equipped the max number of pets")

		return
	end
	local petUniqueName = uniquePetID -- Combine type and ID to create unique pet identifier
	if newPetowner[petUniqueName] then

		local newOwner = newPetowner[petUniqueName]
		for Pet,Owner in pairs(newPetowner) do
			print(player,"NewOwner is now")
			PetFollowPlayer:FireClient(newOwner,playerPets[player.UserId][uniquePetID])
			--equippedPetsCount[newOwner.UserId]+=1
			PetXPSystem:EquipPet(newOwner,playerPets[player.UserId][uniquePetID])
			return
		end

	end
	if playerPets[player.UserId][uniquePetID] then

		print("Pet already exists, equipping existing pet")
		PetFollowPlayer:FireClient(player,playerPets[player.UserId][uniquePetID])
		PetXPSystem:EquipPet(player,playerPets[player.UserId][uniquePetID])
		--equippedPetsCount[player.UserId]+=1
		return
	end
	if not playerPets[player.UserId][uniquePetID] and not newPetowner[petUniqueName] then
		local newPet = OwnedPetsFolder:FindFirstChild(uniquePetID)
		print(petType,player,egg,uniquePetID,"What here",newPet)
		playerPets[player.UserId][uniquePetID] = newPet
		PetFollowPlayer:FireClient(player,playerPets[player.UserId][uniquePetID])
		PetXPSystem:EquipPet(player,playerPets[player.UserId][uniquePetID])
		--equippedPetsCount[player.UserId]+=1
		return

			--playerPets[player.UserId][uniquePetID] =
	end

end
local function CreatePetButton(petType, uniquePetID, petImage, parentFrame, player, egg)
	-- Create the main pet button (ImageButton)
	local AvailablePet = Instance.new("ImageButton")
	AvailablePet.Name = uniquePetID
	AvailablePet.Parent = parentFrame
	AvailablePet.Image = petImage
	AvailablePet.BackgroundTransparency = 1

	-- Create a BoolValue to track whether the pet is equipped
	local BoolValue = Instance.new("BoolValue")
	BoolValue.Name = "Equipped"
	BoolValue.Parent = AvailablePet
	BoolValue.Value = false

	-- Create the Equip button
	local EquipButton = Instance.new("ImageButton")
	EquipButton.Name = "EquipButton"
	EquipButton.Parent = AvailablePet
	EquipButton.Size = UDim2.new(0.3, 0, 0.3, 0)
	EquipButton.Position = UDim2.new(0.8, 0, 0.8, 0)
	--EquipButton.BackgroundColor3 = Color3.new(1, 1, 1)
	EquipButton.BackgroundTransparency = 1
	EquipButton.Image = "rbxassetid://70434054874756"
	EquipButton.Visible = false
	
	--local Status = Instance.new("ImageButton")
	--Status.Name = "PetStatus"
	--Status.Parent = EquipButton
	--Status.Size = UDim2.new(1,0,1,0)
	----Status.BackgroundColor3 = Color3.new(1, 0, 0)
	--Status.TextScaled = true
	--Status.Font = Enum.Font.FredokaOne
	----Status.Text = "Unequipped"

	local AffinityFrame = AffinityFrame:Clone()
	AffinityFrame.Parent = AvailablePet
	AffinityFrame.Position = UDim2.new(0.1, 0, 1.15, 0)
	
	local HeartImg = HeartImage:Clone()
	HeartImg.Parent = AvailablePet
	HeartImg.Position = UDim2.new(0.05, 0, 1.045, 0)
	
	local function UpdateEquipButton()
		if BoolValue.Value == true then
			-- Pet is equipped, set button to "Unequip"
			EquipButton.Visible = true
			--Status.BackgroundColor3 = Color3.new(1, 0, 0)
			--Status.Text = "Un-Equip"
			
			-- If using a TextButton:
			-- EquipButton.Text = "Unequip"
		else
			EquipButton.Visible = false
			--Status.BackgroundColor3 = Color3.new(0, 1, 0.2)
			--Status.Text = "Equip"


		end
	end

	-- Initialize the button appearance
	UpdateEquipButton()
	-- Connect the Equip button logic

	AvailablePet.MouseButton1Click:Connect(function()
		print("Equipping")
		if BoolValue.Value == true then
			-- Pet is currently equipped, so unequip it
			print("Unequipping pet:", petType)
			-- UnEquip the pet
			BoolValue.Value = false
			--   if newPetowner[uniquePetID] then
			--	local newOwner = newPetowner[uniquePetID]
			--     player = newOwner
			--end

			UpdateEquipButton()
			OnUnFollow(player, petType, uniquePetID)

		else
			-- Pet is currently unequipped, so equip it
			print("Equipping pet:", petType)
			-- Ensure the player does not exceed the max equipped pets
			if newPetowner[uniquePetID] then
				local newOwner = newPetowner[uniquePetID]
				print("Owneris new",newOwner)
				player = newOwner
			end
			if not equippedPetsCount[player.UserId] then
				equippedPetsCount[player.UserId] = 0
			end
			print("Player who own this pet",player.Name)
			if equippedPetsCount[player.UserId] >= MaxPetEqquiped then
				warn("You cannot equip more than " .. MaxPetEqquiped .. " pets!"..player.Name)
				return
			end

			-- Equip the pet
			BoolValue.Value = true
			UpdateEquipButton()
			OnFollow(player, petType, egg, uniquePetID)
			print(player,petType,egg,uniquePetID,"Whats Getting")


		end


	end)
end

local function LoadPlayerPets(player)
	local UserId = player.UserId
	local lastLeaveTime
	local success, lastLeaveTimeResult = pcall(function()
		return HatchDataStore:GetAsync("leaveTime"..UserId)
	end)
	if success and lastLeaveTimeResult then
		lastLeaveTime = lastLeaveTimeResult
		print(lastLeaveTime,"When player left")
	else
		lastLeaveTime = os.time() -- Set to current time if no data found
	end
	local currentTime = os.time()
	print(currentTime,"When player joined")
	local timeAway = math.min(currentTime - lastLeaveTime,86400)
	local success,hatchingData = pcall(function()
		return HatchDataStore:GetAsync(UserId)
	end)
	local success,playerPetData = pcall(function()
		return PetdataStore:GetAsync(UserId)
	end)
	print("Success?",success,playerPetData)
	if success and playerPetData then
		playerPets[UserId] = {}
		local playerGui = player:WaitForChild("PlayerGui")
		local petInventory = playerGui:WaitForChild("MainSS"):FindFirstChild("PetInventory")

		for _, petData in pairs(playerPetData) do


			local petType = petData.petType
			local uniquePetID = petData.uniquePetID
			local petSpecie = petData.PetSpecie
			print(petType..uniquePetID,"What in here")
			local Pets = ReplicatedStorage.Pets
			print(Pets:GetChildren())
			for _,PetsM in pairs(Pets:GetChildren()) do 
				print(PetsM.Name,petSpecie,"Name and Specie")
				--print(PetsM.Name == petType,Pets,petType,"Its true now")
				if PetsM.Name == petSpecie then
					print(PetsM,"What type a Pet")
					for i,Pet in pairs(PetsM:GetChildren()) do
						local petName = Pet:WaitForChild("Value").Value

						if petName == petData.name then

							local petModel = Pet:Clone()

							petModel.Name = petData.uniquePetID
							petModel.Parent = OwnedPetsFolder

							print(petModel,"Pet model")
							petModel:SetAttribute("XP", petData.xp or 0)
							petModel:SetAttribute("Level", petData.level or 1)
							petModel:SetAttribute("Affinity", petData.affinity or 0)
							petModel:SetAttribute("Rarity", petData.rarity or "Common")

							playerPets[UserId][uniquePetID] = petModel

							for i,v in pairs(petInventory:GetDescendants()) do
								if v.Name==petData.name then
									CreatePetButton(petData.name,uniquePetID,v.Image,v.Parent,player,playerPetData.rarity)
									task.wait(0.5)
									PetFollowPlayer:FireClient(player,petModel.Name)
									PetXPSystem:EquipPet(player,petModel)
									break
								end
							end	
							PetXPSystem:OnPlayerJoin_StartXP(player, playerPets[UserId][uniquePetID],timeAway)
						end

						-- On player Join the game

					end
				end
			end			
		end
	else
		warn("No Pets Found For Player"..player.Name)
	end
end

LoadPlayerPets is calling on playerAdded
When both players pet traded off they equip/unequip with the player who gets the pet but when any of player leave and rejoins that traded pet doesnt follow cause that Follow event from server(In OnFollow function)doget fire for them, it doesn’t get recieve on Client side for sure i have put prints. And any other event doesn’t recieve from client side for traded pets on rejoining as well. it sending player and pet firing from server accurately but it doesnt get recieve on client end at all with traded pets after rejoining only.

Bug:
https://docs.google.com/uc?export=download&id=1-rQk9ikqDlPJKNHtVpNNId6aT-BsecCX
Gameplay:
https://docs.google.com/uc?export=download&id=1DkHabKGDTTo2p50wHecdH1EnA2_oAbq9

I am making all pet follow on LoadPet thats why it getting follow on start correctly but issue occurs on equipping and unequipping these 4 pets are traded pets with my colleague. which won’t work fine in a same server when leave and rejoin but when we both leave and create a new server it runs fine as usual.

1 Like