Can't select a random item

Local script:

local rp = game:GetService("ReplicatedStorage")
local runSerivce = game:GetService("RunService")
local tweenService = game:GetService("TweenService")
local uis = game:GetService("UserInputService")

local player = game:GetService("Players").LocalPlayer

local things = rp:WaitForChild("EggThings")
local eggs = workspace:WaitForChild("Eggs")
local Module3D = require(rp:WaitForChild("Module3D"))

local maxDisplayDistance = 15
local canHatch = false
local isHatching = false

wait(0.5)

local function animateBillBoard(billboard, openOrClose)
	if openOrClose == true then
		wait(0.4)
		tweenService:Create(billboard, TweenInfo.new(.4), {Size = UDim2.new(9, 0, 9, 0)}):Play()
	else
		tweenService:Create(billboard, TweenInfo.new(.4), {Size = UDim2.new(0, 0, 0, 0)}):Play()
		wait(0.5)
		billboard.Enabled = false
	end
	wait(0.5)
end

for i, v in	 pairs(eggs:GetChildren()) do
	local eggThings = things:FindFirstChild(v.Name)
	
	if eggThings ~= nil then
		
		local billboardTemp = script.Template:Clone()
		local container = billboardTemp:WaitForChild("Container")
		local container1 = container.Container1
		local display = container1:WaitForChild("Display")
		
		billboardTemp.Parent = script.Parent.Parent.EggBillboards
		billboardTemp.Name = v.Name
		billboardTemp.Adornee = v.Egg_1
		billboardTemp.Enabled = true
		
		local things = {}
		
		for x, thing in pairs(eggThings:GetChildren()) do
			table.insert(things, thing.Rarity.Value)
		end
		
		table.sort(things)
		
		for _, rarity in pairs(things) do
			for _, thing in pairs(eggThings:GetChildren()) do
				if thing.Rarity.Value == rarity then 
					wait(0.1)

					if thing.Rarity.Value == 50 then
						local rarity = thing.Rarity.Value


						local clonedTemp1 = container1:Clone()

						clonedTemp1.Name = thing.Name
						clonedTemp1.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
						clonedTemp1.Visible = true
						clonedTemp1.Parent = container

						local thingModel = Module3D:Attach3D(clonedTemp1.Display,thing:Clone())
						thingModel:SetDepthMultiplier(1.2)
						thingModel.Camera.FieldOfView = 5 
						thingModel.Visible = true

						runSerivce.RenderStepped:Connect(function()
							thingModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
						end)

					end

					if thing.Rarity.Value == 35 then
						local clonedTemp2 = container.Container2:Clone()

						clonedTemp2.Name = thing.Name
						clonedTemp2.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
						clonedTemp2.Visible = true
						clonedTemp2.Parent = container

						local thingModel = Module3D:Attach3D(clonedTemp2.Display,thing:Clone())
						thingModel:SetDepthMultiplier(1.2)
						thingModel.Camera.FieldOfView = 5 
						thingModel.Visible = true

						runSerivce.RenderStepped:Connect(function()
							thingModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
						end)

					end

					if thing.Rarity.Value == 14 then
						local clonedTemp3 = container.Container3:Clone()

						clonedTemp3.Name = thing.Name
						clonedTemp3.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
						clonedTemp3.Visible = true
						clonedTemp3.Parent = container

						local thingModel = Module3D:Attach3D(clonedTemp3.Display,thing:Clone())
						thingModel:SetDepthMultiplier(1.2)
						thingModel.Camera.FieldOfView = 5 
						thingModel.Visible = true

						runSerivce.RenderStepped:Connect(function()
							thingModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
						end)

					end 

					if thing.Rarity.Value == 1 then
						local clonedTemp4 = container.Container4:Clone()

						clonedTemp4.Name = thing.Name
						clonedTemp4.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
						clonedTemp4.Visible = true
						clonedTemp4.Parent = container

						local thingModel = Module3D:Attach3D(clonedTemp4.Display,thing:Clone())
						thingModel:SetDepthMultiplier(1.2)
						thingModel.Camera.FieldOfView = 5 
						thingModel.Visible = true

						runSerivce.RenderStepped:Connect(function()
							thingModel:SetCFrame(CFrame.Angles(0,tick() % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
						end)

					end

					wait(0.5)

					--if thing.Rarity.Value == 0.04 then
					--	local clonedTemp5 = container.Container5:Clone()

					--	clonedTemp5.Name = thing.Name
					--	clonedTemp5.Rarity.Text = tostring(thing:WaitForChild("Rarity").Value).. "%"
					--	clonedTemp5.Visible = true
					--	clonedTemp5.Parent = container

					--	print(clonedTemp5, thing.Rarity.Value)
					--end
					break
				else
					continue
				end
			end  
		end
		
		runSerivce.RenderStepped:Connect(function()
			if player:DistanceFromCharacter(v.Core.Position) < maxDisplayDistance then
				canHatch = true
				billboardTemp.Enabled = true
				animateBillBoard(billboardTemp, true)
			else
				animateBillBoard(billboardTemp, false)
				canHatch = false
			end
		end)
	end
end


local function hatchOne(thing, egg)
	print(thing)
end


uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if canHatch == true and player.Character ~= nil and isHatching == false then
			local nearestEgg
			local plrPos = player.Character.HumanoidRootPart.Position
			
			for i, v in pairs(eggs:GetChildren()) do
				if nearestEgg == nil then
					nearestEgg = v
				else
					if v.PrimaryPart and (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
						print(v)
						nearestEgg = v
					end
				end
			end
			
			local result = rp:WaitForChild("HatchServer"):InvokeServer(nearestEgg)
			
			if result ~= nil then
				isHatching = true
				hatchOne(result, nearestEgg)
				wait(3) 
				isHatching = false
			else
				print("cannot hatch")
			end
		else
			print("Can't hatch")
		end
	end
end)

Script:

game.ReplicatedStorage.HatchServer.OnServerInvoke = function(player, egg)
	local eggModel = workspace.Eggs:FindFirstChild(egg.Name)
	
	if eggModel ~= nil then
		local price = eggModel.Price.Value
		local currency = eggModel.Currency.Value
		
		if player.leaderstats[currency].Value >= price then
			local chosenPet = game.ReplicatedStorage.EggThings[eggModel.Name]:GetChildren(math.random(1, #game.ReplicatedStorage.EggThings[egg.Name]:GetChildren()))
			return chosenPet
		else
			return false
		end
	end
end

So what’s the problem? The problem is that my task is to choose a random item from the egg, as in the guide at the 4th minute. There when you click it gets a random pet in output, but I have for some reason written all things, here’s looking:

As I understand the problem is that the ChosenPets, I choose were somehow wrong, but how exactly I do not know.

2 Likes