I need help for hatching egg thing (pros only)

ok so basically i was trying some stuff and then i found this script:

--- Variables ---

local PetsFolder = game.ReplicatedStorage:FindFirstChild("PetsFolder")
local PetsModule = require(game.ServerScriptService.PetModule)
local BadgeService = game:GetService("BadgeService")

local PlayersUsing = {}
local Price = 500

-- Dictionary of pet names to their corresponding badge IDs
local PetBadges = {
	["pet_one"] = 2126467025449246,
	["pet_two"] = 567237262215735,
	["pet_three"] = 2543500802008691 -- Add entries for all pets
}

--- Buy Egg ---

script.Parent.ClickDetector.MouseClick:Connect(function(Player)

	local Character = Player.Character

	if PlayersUsing[Player] then 
		return 
	end

	PlayersUsing[Player] = true

	if Player.leaderstats.Gems.Value >= Price then
		Player.leaderstats.Gems.Value -= Price

		local PetsTable = {}

		-- Populate PetsTable based on rarity
		for _, Pet in pairs(PetsFolder:GetChildren()) do
			local Rarity = Pet.Rarity.Value

			for i = 1, PetsModule.Rarities[Rarity] do
				table.insert(PetsTable, Pet)
			end
		end

		local ChosenPet = PetsTable[math.random(1, #PetsTable)]
		local PetClone = ChosenPet:Clone()

		local HatchingGui = Player.PlayerGui:WaitForChild("HatchingGui")
		local EggImage = HatchingGui:FindFirstChild("Frame"):WaitForChild("EggImage")
		EggImage.Visible = true

		local Tick = tick()
		local Timer = 3

		-- Hatching Animation
		while tick() - Tick < Timer do
			EggImage.Rotation = 13
			script.Parent.sound.click:Play()
			wait(0.05)

			EggImage.Rotation = 0
			script.Parent.sound.click:Play()
			wait(0.05)

			EggImage.Rotation = -13
			script.Parent.sound.click:Play()
			wait(0.05)

			EggImage.Rotation = 0
			script.Parent.sound.click:Play()
			wait(0.05)
		end

		EggImage.Visible = false
		local PetImage = HatchingGui:FindFirstChild("Frame").PetImages:FindFirstChild(ChosenPet.Name)
		PetImage.Visible = true

		script.Parent.sound.eggsound:Play()
		wait(3)
		PetImage.Visible = false

		PlayersUsing[Player] = false

		-- Determine position for the new pet
		local existingPets = Character:GetChildren()
		local petSpacing = 5 -- Adjust spacing between pets
		local newPetPosition = Vector3.new(-5, 0, 5) -- Starting position offset for the first pet

		if #existingPets > 0 then
			for _, existingPet in pairs(existingPets) do
				if existingPet:IsA("Model") and PetBadges[existingPet.Name] then
					newPetPosition = newPetPosition + Vector3.new(petSpacing, 0, 0) -- Move position for each existing pet
				end
			end
		end

		-- Clone and position the new pet
		PetClone.Parent = Character
		PetClone:SetPrimaryPartCFrame(Character.HumanoidRootPart.CFrame * CFrame.new(newPetPosition))

		-- Create BodyPosition and BodyGyro to follow the character
		local BodyPosition = Instance.new("BodyPosition", PetClone.MainPart)
		BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

		local BodyGyro = Instance.new("BodyGyro", PetClone.MainPart)
		BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

		-- Update the pet's position and orientation
		while wait() do
			BodyPosition.Position = Character.HumanoidRootPart.Position + newPetPosition
			BodyGyro.CFrame = Character.HumanoidRootPart.CFrame
		end

		-- Award the badge for this pet if it hasn't been awarded yet
		local BadgeID = PetBadges[ChosenPet.Name]
		if BadgeID and not Player:HasBadge(BadgeID) then
			local success, err = pcall(function()
				BadgeService:AwardBadge(Player.UserId, BadgeID)
			end)

			if not success then
				warn("Failed to award badge: " .. err)
			end
		end
	end
end)

what it does is when egg is clicked, it bases on a rarity module of pets and gives a pet, attacked to character,

not working: that it doesnt work the badge giver for every pet and that the pet doesnt remain if player die (its ok if u cant do nothing about it)

note: theres a value in player that says what pet is equipped in that moment

if someone would send a revisited code i would be glad to him :slight_smile:

Screenshot 2024-08-21 155137
in replicatedstorage*

1 Like
script.Parent.ClickDetector.MouseClick:Connect(function(Player)

		...
		
		-- Clone and position the new pet
		PetClone.Parent = workspace
		PetClone:SetPrimaryPartCFrame(Character.HumanoidRootPart.CFrame * CFrame.new(newPetPosition))

		-- Create BodyPosition and BodyGyro to follow the character
		local BodyPosition = Instance.new("BodyPosition", PetClone.MainPart)
		BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

		local BodyGyro = Instance.new("BodyGyro", PetClone.MainPart)
		BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

		-- Update the pet's position and orientation
		task.spawn(function()
			while task.wait() do
				if Character then
					BodyPosition.Position = Character.HumanoidRootPart.Position + newPetPosition
					BodyGyro.CFrame = Character.HumanoidRootPart.CFrame
				end
			end
		end)
		
		-- Update character reference on respawn
		Player.CharacterAdded:Connect(function(newCharacter)
			Character = newCharacter
		end)
		
		...
		
	end
end)

This code should fix your issue with the pets disappearing on respawn.

Does it only work for some of them? Does an error appear if you try to give the badge? Does the badge-giving code even run at all?

thanks for the script, the badge thing actually doesnt do any error idk if it is running correctly

Can you see which of these print:

		...
		
		-- Award the badge for this pet if it hasn't been awarded yet
		local BadgeID = PetBadges[ChosenPet.Name]
		print(BadgeID)
		print(Player:HasBadge(BadgeID))
		if BadgeID and not Player:HasBadge(BadgeID) then
			print("giving badge")
			local success, err = pcall(function()
				BadgeService:AwardBadge(Player.UserId, BadgeID)
			end)
			print(success, err)

			if not success then
				warn("Failed to award badge: " .. err)
			end
		end

if u still alive,

well it doesnt find the “HasBadge” thing

update: i fixed that but the position still isnt right

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.