Egg Hatching System Bug

So basically i made a egg hatching system,it worked perfectly fine for a week so as you can see in the gyazo,but then when i tryed to test it a week later everytime i hatched it everything was in a weird position or the size of the ui was messed up(gyazo 2)


local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

local OpenEggUI = PlayerGui:WaitForChild("OpenEgg")
local HatchingEgg = OpenEggUI:WaitForChild("HatchingEgg")
local Contents = OpenEggUI:WaitForChild("Contents")
local EggToBeHatched = HatchingEgg:WaitForChild("EggToBeHatched")
local PetViewport = OpenEggUI:WaitForChild("Pet")
local Location = PetViewport:WaitForChild("Location")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = ReplicatedStorage:WaitForChild("Debris")
local EggsFolder = Debris:WaitForChild("Eggs")
local Audio = Debris:WaitForChild("Audio")
local Events = ReplicatedStorage:WaitForChild("Events")
local Pets = ReplicatedStorage:WaitForChild("Pets")
local Modules = ReplicatedStorage:WaitForChild("Modules")

local TweenService = game:GetService("TweenService")

local EggRaritiesModule = require(Modules.EggRarities)

local UIS = game:GetService("UserInputService")

local FalseEgg = EggsFolder:WaitForChild("FalseEgg")

local DurationOfTween = .75
local OrientationX = 15
local TimesToShake = 15
local ShowcaseWait = 3

local isOpening = false
local isOpeningBulk = true
local BulkCount = 0
local MaxBulkCount = 3

local TestingEnabled = true

local SinglePurchase = false

local MagnitudeCloseness = 15

local isClose = false

local Debounce = false
local DebounceTimer = 3

local function RainbowText(Text)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Text, tweenInfo, {TextColor3 = Color3.fromRGB(197, 0, 0)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Text, tweenInfo, {TextColor3 = Color3.fromRGB(197, 132, 20)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Text, tweenInfo, {TextColor3 = Color3.fromRGB(181, 197, 39)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Text, tweenInfo, {TextColor3 = Color3.fromRGB(35, 197, 76)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Text, tweenInfo, {TextColor3 = Color3.fromRGB(29, 40, 197)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Text, tweenInfo, {TextColor3 = Color3.fromRGB(35, 197, 76)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Text, tweenInfo, {TextColor3 = Color3.fromRGB(29, 40, 197)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Text, tweenInfo, {TextColor3 = Color3.fromRGB(29, 40, 197)}) tween:Play()
end

local function RainbowImage(Image)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Image, tweenInfo, {ImageColor3 = Color3.fromRGB(197, 0, 0)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Image, tweenInfo, {ImageColor3 = Color3.fromRGB(197, 132, 20)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Image, tweenInfo, {ImageColor3 = Color3.fromRGB(181, 197, 39)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Image, tweenInfo, {ImageColor3 = Color3.fromRGB(35, 197, 76)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Image, tweenInfo, {ImageColor3 = Color3.fromRGB(29, 40, 197)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Image, tweenInfo, {ImageColor3 = Color3.fromRGB(35, 197, 76)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Image, tweenInfo, {ImageColor3 = Color3.fromRGB(29, 40, 197)}) tween:Play()
	wait(.5)
	local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(Image, tweenInfo, {ImageColor3 = Color3.fromRGB(29, 40, 197)}) tween:Play()
end

local function CloneEgg(EggName)
		if isOpening == false then
			local ReturnedEgg
			local SelectedPet
			isOpening = true
			HatchingEgg.Visible = true
			EggToBeHatched:ClearAllChildren()
			local SearchForEgg = EggsFolder:WaitForChild(EggName)
			if SearchForEgg ~= nil then
				local Clone = SearchForEgg:Clone()
				Clone.Parent = EggToBeHatched
				ReturnedEgg = Clone
				SelectedPet = EggRaritiesModule.FindPet(EggName)
			end
			if ReturnedEgg ~= nil then
				local PetClone = Pets:WaitForChild(SelectedPet):Clone()
				PetClone.Parent = Player:WaitForChild("PlayerPets")
				return ReturnedEgg, SelectedPet
			end
		end
		if isOpening == true and isOpeningBulk == true then
			local ReturnedEgg
			local SelectedPet
			isOpening = true
			HatchingEgg.Visible = true
			EggToBeHatched:ClearAllChildren()
			local SearchForEgg = EggsFolder:WaitForChild(EggName)
			if SearchForEgg ~= nil then
				local Clone = SearchForEgg:Clone()
				Clone.Parent = EggToBeHatched
				ReturnedEgg = Clone
				SelectedPet = EggRaritiesModule.FindPet(EggName)
			end
			if ReturnedEgg ~= nil then
				return ReturnedEgg, SelectedPet
			end
	end
end

local function Shake(Egg, Times, IsNegative)
	HatchingEgg.Visible = true
	if IsNegative == false then
		FalseEgg.Orientation = Vector3.new(OrientationX, 0, 0)
		Egg:SetPrimaryPartCFrame(FalseEgg.CFrame)
		wait(DurationOfTween / Times)
	elseif IsNegative == true then
		FalseEgg.Orientation = Vector3.new(-OrientationX, 0, 0)
		Egg:SetPrimaryPartCFrame(FalseEgg.CFrame)
		wait(DurationOfTween / Times)
	end
	Audio:WaitForChild("Short Shake"):Play()
end

local function ClonePet(PetName)
	local SelectedPet
	Location:ClearAllChildren()
	local SearchForPets = Pets:WaitForChild(PetName)
	if SearchForPets ~= nil then
		local Clone = SearchForPets:Clone()
		Clone.Parent = Location
		Clone.PrimaryPart = Clone:WaitForChild("Base")
		SelectedPet = Clone
	end
	if SelectedPet ~= nil then
		return SelectedPet
	end
end

local function TweenEgg(Egg, SelectedPet, isBulk)
	if Egg ~= nil then
		if isBulk == false then
			local Number = 2
			local IsNegative = true
			HatchingEgg.Visible = true
			PetViewport.Visible = true
			Contents.Visible = false
			for i = 1, TimesToShake do
				Shake(Egg, Number, IsNegative)
				Number = Number + 1
				IsNegative = not IsNegative
			end
			FalseEgg.Orientation = Vector3.new(0, 0, 0)
			Egg:SetPrimaryPartCFrame(FalseEgg.CFrame)
			Audio:WaitForChild("Short Shake"):Play()
			wait(DurationOfTween / 10)
			local tweenInfo = TweenInfo.new(.25, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(HatchingEgg, tweenInfo, {Size = UDim2.new(4, 0, 4, 0)}) tween:Play()
			local tweenInfo = TweenInfo.new(.25, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(HatchingEgg, tweenInfo, {ImageTransparency = 1}) tween:Play()
			Contents.Visible = true
			Audio:WaitForChild("Sparkle"):Play()
			Contents:WaitForChild("PetName").Text = SelectedPet
			local PetRarity = EggRaritiesModule.GetRarityByName(SelectedPet)
			Contents:WaitForChild("Rarity").Text = PetRarity
			local DetermineRarityColor 
			
			DetermineRarityColor(Contents:WaitForChild("Rarity"), PetRarity)
			local ClonedPet = ClonePet(SelectedPet)
			wait(ShowcaseWait)
			PetViewport:TweenPosition(UDim2.new(.521, 0, 1.4, 0), "Out", "Linear", .75)
			Contents.Visible = false
			wait(.75)
			ClonedPet:Destroy()
			PetViewport.Position = UDim2.new(0.521, 0, 0.467, 0)
			EggToBeHatched:ClearAllChildren()
			HatchingEgg.Size = UDim2.new(1, 0, 1, 0)
			HatchingEgg.ImageTransparency = 0
			HatchingEgg.Visible = false
			Contents:WaitForChild("Rarity").Text = ""
			wait(.25)
			isOpening = false
		elseif isBulk == true then
			local Number = 2
			local IsNegative = true
			HatchingEgg.Visible = true
			PetViewport.Visible = true
			Contents.Visible = false
			BulkCount = BulkCount + 1
			for i = 1, TimesToShake do
				Shake(Egg, Number, IsNegative)
				Number = Number + 1
				IsNegative = not IsNegative
				Audio:WaitForChild("Short Shake"):Play()
			end
			FalseEgg.Orientation = Vector3.new(0, 0, 0)
			Egg:SetPrimaryPartCFrame(FalseEgg.CFrame)
			Audio:WaitForChild("Short Shake"):Play()
			wait(DurationOfTween / 10)
			local tweenInfo = TweenInfo.new(.25, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(HatchingEgg, tweenInfo, {Size = UDim2.new(4, 0, 4, 0)}) tween:Play()
			local tweenInfo = TweenInfo.new(.25, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0 ) local tween = TweenService:Create(HatchingEgg, tweenInfo, {ImageTransparency = 1}) tween:Play()
			Contents.Visible = true
			Audio:WaitForChild("Sparkle"):Play()
			Contents:WaitForChild("PetName").Text = SelectedPet
			local PetRarity = EggRaritiesModule.GetRarityByName(SelectedPet)
			Contents:WaitForChild("Rarity").Text = PetRarity
			local ClonedPet = ClonePet(SelectedPet)
			wait(ShowcaseWait)
			PetViewport:TweenPosition(UDim2.new(.521, 0, 1.4, 0), "Out", "Linear", .75)
			Contents.Visible = false
			wait(.75)
			ClonedPet:Destroy()
			PetViewport.Position = UDim2.new(0.521, 0, 0.467, 0)
			EggToBeHatched:ClearAllChildren()
			HatchingEgg.Size = UDim2.new(1, 0, 1, 0)
			HatchingEgg.ImageTransparency = 0
			HatchingEgg.Visible = false
			Contents:WaitForChild("Rarity").Text = ""
			wait(.25)
			if BulkCount < MaxBulkCount then
				isOpening = true
				isOpeningBulk = true
				local NewEgg, NewPet = CloneEgg(Egg.Name)
				TweenEgg(NewEgg, NewPet, true)
			elseif BulkCount >= MaxBulkCount then
				isOpening = false
				isOpeningBulk = false
				BulkCount = 0
			end
			wait(DebounceTimer)
			Debounce = false
		end
	end
end

Contents:GetPropertyChangedSignal("Visible"):Connect(function()
	if Contents.Visible == true then
		for i = 1, 180 do
			Contents:WaitForChild("Sunburst").Rotation = Contents:WaitForChild("Sunburst").Rotation + 1
			wait(0.01)
		end
	end
end)

Contents:WaitForChild("Rarity"):GetPropertyChangedSignal("Text"):Connect(function()
	local Rarity = Contents:WaitForChild("Rarity").Text
	local Text = Contents:WaitForChild("Rarity")
	if Rarity == "Common" then
		Text.TextColor3 = Color3.fromRGB(145, 123, 123)
		Contents:WaitForChild("Sunburst").ImageColor3 = Color3.fromRGB(145, 123, 123)
	elseif Rarity == "Uncommon" then
		Text.TextColor3 = Color3.fromRGB(117, 197, 137)
		Contents:WaitForChild("Sunburst").ImageColor3 = Color3.fromRGB(117, 197, 137)
	elseif Rarity == "Rare" then
		Text.TextColor3 = Color3.fromRGB(76, 145, 197)
		Contents:WaitForChild("Sunburst").ImageColor3 = Color3.fromRGB(76, 145, 197)
	elseif Rarity == "Epic" then
		Text.TextColor3 = Color3.fromRGB(125, 24, 197)
		Contents:WaitForChild("Sunburst").ImageColor3 = Color3.fromRGB(125, 24, 197)
	elseif Rarity == "Legendary" then
		RainbowText(Text)
		RainbowImage(Contents:WaitForChild("Sunburst"))
	elseif Rarity == "???" then
		RainbowText(Text)
		RainbowImage(Contents:WaitForChild("Sunburst"))
	end
end)

local Capsules = workspace.Capsules

Location.ChildAdded:Connect(function(Pet)
	if Pet:IsA("Model") and Pet.PrimaryPart ~= nil then
		for i = 1, 150 do
			if Pet:IsA("Model") and Pet.PrimaryPart ~= nil then
				Pet:SetPrimaryPartCFrame(Pet.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(i / 20), 0))
				wait(0.001)
			end
		end
	end
end)
--TweenEgg(CloneEgg("Carn"))


local SelectedEgg

game:GetService("RunService").RenderStepped:Connect(function()
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local HRP = Character:WaitForChild("HumanoidRootPart")
	if (HRP.Position - Capsules.BasicEggCapsule.PrimaryPart.Position).magnitude <= MagnitudeCloseness then
		SelectedEgg = "Basic_Egg"
	elseif (HRP.Position - Capsules.LavaCapsule.PrimaryPart.Position).magnitude <= MagnitudeCloseness then
		SelectedEgg = "Lava"
	elseif (HRP.Position - Capsules.CarnCapsule.PrimaryPart.Position).magnitude <= MagnitudeCloseness then
		SelectedEgg = "Carn"
	else
		SelectedEgg = nil
	end
end)

UIS.InputBegan:Connect(function(Input, gameProcessed)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local HRP = Character:WaitForChild("HumanoidRootPart")
		if HRP then
		if not gameProcessed then
			if Input.KeyCode == Enum.KeyCode.E then
				if TestingEnabled == true then
					if isOpening == false then
						if SelectedEgg ~= nil then
							isOpeningBulk = false
							local Egg, RandomPet = CloneEgg(SelectedEgg)
							TweenEgg(Egg, RandomPet, false)
						end
					end
				end
			end
		end
	end
end)


I have an error [17:09:22.545 - Players.EngineScript.PlayerGui.OpenEgg.HatchingEgg.Hatching:178: attempt to call a number value]

DetermineRarityColor(Contents:WaitForChild("Rarity"), PetRarity)

I changed line 178 to local DetermineRarity to local DetermineRarity with nothing

2 Likes

Well the issue is exactly as it says, you’re trying to call a number value:

local DetermineRarityColor = 1 --line 176

DetermineRarityColor(Contents:WaitForChild("Rarity"), PetRarity) --line 178

Perhaps you accidently removed some code in the gap between those two?

2 Likes

Nope i didnt but as you can see,I have module for pet rarities and a text gui named PetRaritys

it says im calling a nil value

DetermineRarityColor(Contents:WaitForChild("Rarity"), PetRarity)

1 Like

That’s because you didn’t define the function DetermineRarityColor anywhere besides the local DetermineRarityColor = 1, which doesn’t make any sense.

Where is the function supposed to be defined?

4 Likes