Egg hatching system pet position off

I made an egg hatching system and made it compatible with multi eggs, but when I do it in multi eggs, the positions are all off and when I do single egg, everything is just fine.

Single egg:

Multi egg (Sorry for lag)

Script:

local ts = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")
local eggs = require(rs:WaitForChild("modules"):WaitForChild("eggs"))
local hatch = rs:WaitForChild("events"):WaitForChild("egg")
local http = game:GetService("HttpService")
local allpetshatchedurl = "https://discordapp.com/api/webhooks/679544252761636909/wOkdALv3vB1h_acaSqQamlGfKMZoi7H-1p6nOjWinNhiG8-OefGS3bekvjQ5Y5dPyIIs"
local cost
local data
local currency
local library = rs:WaitForChild("petlibrary")
local secretpetshatchedurl = "https://discordapp.com/api/webhooks/674765704347779072/gVY5RLjOa7-bzrfa_GUGXuQ1MBODCaorEBq9yGMrPcBocfSMXzi8nnKT203fGnrl9v31"

local totalWeight = 0

local function postNormPet(player,petName,rarity)
	local data = {
		["embeds"] = {{
				["title"] = "Pet Hatched",
				["description"] = "**"..player.."** just hatched a "..rarity.." **"..petName.."**!",
				["color"] = 44543,
    	}}
	}
	local finalData = http:JSONEncode(data)
	http:PostAsync(allpetshatchedurl,finalData)
end

local function legendHatched(player,petName)
	player.PlayerGui.lcan.petName.Value = petName
end

local function secretPetPost(playerName,petName)
	local img
	if petName == "Lord cube" then
		img = "https://www.bing.com/images/blob?bcid=TwIxvWwldgUB6Q"
	end
	local data = {
		["embeds"] = {{
				["title"] = "__**Secret Pet Hatched**__",
				["description"] = "Someone just hatched a **"..petName.."**.",
				["color"] = 44543,
				["thumbnail"] = {
					["url"] = img
				}
    	}}
	}
	local finalData = http:JSONEncode(data)
	http:PostAsync(secretpetshatchedurl,finalData)
end

local function rotateTween(Obj,Rot)
	local ti = TweenInfo.new(.1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
	local properties = {
		Rotation = Rot
	}
	local Tween = ts:Create(Obj,ti,properties)
	Tween:Play()
end

local function ChoosePet(pets)
	for i,v in pairs(pets) do
		totalWeight = totalWeight + v[1]
	end 
	local chance = math.random(1,totalWeight)
	local counter = 0
	for i,v in pairs(pets) do
		counter = counter+v[1]
		if chance <= counter then
			return i
		end
	end
end

local function unboxPet(Player, eggName, multi)
	local pets
	pets = data["Pets"]
	Player.PlayerGui.eg.Enabled = false
	local egs = {}
	local vpeg = {}
	local pcs = {}
	local num = 1
	if multi == true then
		num = 3
	end
	Player.PlayerGui.petinv.Enabled = false
	Player.PlayerGui.index.Enabled = false
	local pos = {0,-0.3,0.3}
	local rotz = {0,-30,30}
	local petInv = Player:WaitForChild("Pets")
	local GUI = Instance.new("ScreenGui")
	GUI.Name = "hatching"
	for i = 1,num do
	local petChosen = ChoosePet(pets)
	table.insert(pcs,i,petChosen)
	local invp = library:WaitForChild(pcs[i]):Clone()
	invp.Parent = petInv
	print(Player.Name.." Hatched a "..petChosen)
	local viewPort = Instance.new("ViewportFrame")
	viewPort.Name = "a"..i
	viewPort.Size = UDim2.new(1,0,1,0)
	viewPort.BackgroundTransparency = 1
	viewPort.Position = UDim2.new(pos[i],0,-1.1,0)
	local eggClone = workspace.eggs[eggName][eggName]:Clone()
	eggClone.Parent = viewPort
	eggClone:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,0)))
	local Camera = Instance.new("Camera")
	Camera.Parent = viewPort
	Camera.Name = "cam"
	viewPort.CurrentCamera = Camera
	Camera.CFrame = CFrame.new(Vector3.new(0,0,5))
	GUI.Parent = Player.PlayerGui
	viewPort.Parent = GUI
	totalWeight = 0
	eggClone.PrimaryPart.Orientation = Vector3.new(0,rotz[i],0)
	table.insert(vpeg,i,eggClone)
	end
	local vps = {}
	for i,c in pairs(Player.PlayerGui.hatching:GetChildren()) do
		table.insert(vps,i,c)
	end
	for i = 1,num do
		local v = vps[i]
		v:TweenPosition(UDim2.new(pos[i],0,0,0),"Out",Enum.EasingStyle.Bounce,1)
	end
	wait(1)
	local wt = 0.36
	for i = 1,8 do
		for i = 1,num do
		rotateTween(vps[i],25)
		end
		wait(wt)
		wt = wt - 0.02
		for i = 1,num do
		rotateTween(vps[i],-25)
		end
		wait(wt)
		wt = wt - 0.02
	end
	for i = 1,num do
		rotateTween(vps[i],0)
	end
	wait(.1)
	for i = 1,num do
		for b,c in pairs(vps[i]:GetChildren()) do
			if c.Name ~= "cam" then
			c:Destroy()
			end
		end
	end
	local cams = {}
	for i = 1,num do
		for b,c in pairs(vps[i]:GetChildren()) do
			table.insert(cams,b,c)
		end
	end
	for i = 1,num do
		local petClone = library:WaitForChild(pcs[i]):Clone()
		local headPos = petClone.PrimaryPart.Position
		petClone.Parent = vps[i]
		petClone.PrimaryPart.Orientation = Vector3.new(0,rotz[i],0)
		local Camera = cams[i]
		Camera.CFrame = CFrame.new(Vector3.new(headPos.X,headPos.Y,headPos.Z+7),headPos)
	end
	local txts = {}
	local nb = 0
	local rtys = {}
	for i = 1,num do
		nb = nb + 1
		local cla = script["name"..i]:Clone()
		cla.Parent = Player.PlayerGui.hatching
		cla.Text = pcs[i]
		table.insert(txts,nb,cla)
		nb = nb + 1
		local clb = script["rarity"..i]:Clone()
		clb.Parent = Player.PlayerGui.hatching
		clb.Text = pets[pcs[i]][6]
		table.insert(txts,nb,clb)
		if clb.Text == "Uncommon" then
			clb.TextColor3 = Color3.fromRGB(0,170,255)
			table.insert(rtys,i,"Uncommon")
		elseif clb.Text == "Rare" then
			clb.TextColor3 = Color3.fromRGB(255,170,0)
			table.insert(rtys,i,"Rare")
		elseif clb.Text == "Epic" then
			clb.TextColor3 = Color3.fromRGB(170,0,255)
			table.insert(rtys,i,"Epic")
		elseif clb.Text == "Legendary" or clb.Text == "???" then
			clb.TextColor3 = Color3.fromRGB(0,255,0)
			table.insert(rtys,i,"Legendary")
			legendHatched(Player,pcs[i])
			wait(.2)
		else
			table.insert(rtys,i,"Common")
		end
		if clb.Text == "???" then
			secretPetPost(Player.Name,pcs[i])
		end
	end
	for i = 1,5 do
		for i = 1,(2*num) do
			txts[i].TextTransparency = txts[i].TextTransparency - 0.2
			wait()
		end
	end
	wait(2)
	for i = 1,num do
		local viewPort = vps[i]
		viewPort:TweenPosition(UDim2.new(pos[i],0,1,0),"In","Linear",1)
	end
	for i = 1,5 do
		for i = 1,(2*num) do
			txts[i].TextTransparency = txts[i].TextTransparency + 0.2
			wait()
		end
	end
	for i = 1,num do
		postNormPet(Player.Name,pcs[i],rtys[i])
	end
	Player.PlayerGui.hatching:Destroy()
	Player.PlayerGui.eg.Enabled = true
	Player.PlayerGui.petinv.Enabled = true
	Player.PlayerGui.index.Enabled = true
end

hatch.OnServerEvent:Connect(function(user, player, eggname, tpe)
	data = eggs[eggname]
	cost = data["Cost"]
	currency = data["Currency"]
	local stats = user:WaitForChild("leaderstats")
	local cc = stats:FindFirstChild(currency)
	if cc then
		if cc.Value >= cost then
			user.PlayerGui.eg.Enabled = false
			if tpe == "s" then
				cc.Value = cc.Value - cost
				unboxPet(user,eggname)
			elseif tpe == "m" then
				cc.Value = cc.Value - cost*3
				unboxPet(user,eggname,true)
			elseif tpe == "a" then
				local a = false
				local opening = false
				while a == false do
					if opening == false then
						cc.Value = cc.Value - cost*3
						unboxPet(user,eggname,true)
						opening = true
						wait()
						opening = false
					end
					if (user.Character.HumanoidRootPart.Position-workspace.eggs[eggname].buy.Position).Magnitude > 6 then
						a = true
					end
					wait(.1)
				end
			end
			data = nil
			cost = nil
			pets = nil
			currency = nil
			totalWeight = 0
		end
	end
end)
1 Like

I have no idea why the video is that bad though, sorry for quality :V

you using insert ui or show ui that you already made inside StarterGui? if you using ui from StarterGui then you just have to use this plugin: https://www.roblox.com/library/1496745047/AutoScale-Lite and then is should work. :slight_smile:

Well I’m Inserting the gui from creating it with Instance.new()

1 Like

You have ~25 lines of code creating all of that GUI.

You could simply make the frames Visible. My guess is that some lag actually stems from the game creating all of that GUI stuff. Refrain from creating all that GUI using Instance.new(), as more problems arise if you wish to hatch more pets… What if you’re planning on hatching 10 pets at once? 20 at once? It’s so much strain for no reason.

Create the GUI beforehand and then make it visible; then run the scripts you normally would. It’s possible this may answer your question.

2 Likes

Okay, I’ll try to do that and tell you if that works!