Hello. I am making an egg-hatching system for my game, and I noticed that every time I leave / hatch an egg, the game for some reason doesn’t just run the code again ONCE, it runs it +1 every time.
Ex: I open 2 pets, the 3rd time I open 1 EGG I get 3 pets instead of just 1, then at the 4th egg, I get 4 PETS/egg instead of 1/ egg
I literally tried every single debounce, everything.
I’m sure it’s a stupid mistake I made but please, please help me.
Here’s the entire code
local isHatching = true
for i, eggStand in pairs(workspace.Eggstands:GetChildren()) do
for _, hitBox in pairs(eggStand:GetChildren()) do
if hitBox.Name == "Main" then
local openZone = Zone.new(hitBox)
local hitBoxDebounce = true
local index = 0
openZone.localPlayerEntered:Connect(function()
warn("entered")
if hitBoxDebounce and plr.Character then
hitBoxDebounce = false
index += 1
print(index)
for i, gui in pairs(plr.PlayerGui.EggBillboards:GetChildren()) do gui.Enabled = false end
isHatching = true
local gui = plr.PlayerGui.EggBillboards[eggStand.Name]
gui.Enabled = true
local inputted
local pressedHatchBtn
local leftArea
local function hatch()
local price = gui:GetAttribute("Price")
warn("ran")
if plr.leaderstats.Coins.Value >= price then
local isSuccess,chosenPet = replicatedStorage.Remotes.Hatch:InvokeServer(eggStand.Name,price)
warn(chosenPet)
if isSuccess and chosenPet then
local hatchUI = script:WaitForChild("Opening"):Clone()
hatchUI.Parent = plr.PlayerGui
hatchUI.Enabled = true
gui.Enabled = false
local ogspeed
if plr.Character and plr.Character:FindFirstChild("Humanoid") then
ogspeed = plr.Character.Humanoid.WalkSpeed
plr.Character.Humanoid.WalkSpeed = 0
end
TweenService:Tween(hatchUI.Frame.Egg.UIScale,TweenInfo.new(0.5,Enum.EasingStyle.Back,Enum.EasingDirection.Out,1,true),{
Scale = 1
},true)
TweenService:Tween(hatchUI.Frame.Egg.UIScale,TweenInfo.new(0.5,Enum.EasingStyle.Back,Enum.EasingDirection.Out),{
Scale = 1
})
task.wait(.25)
hatchUI.Frame.Cracked.Visible = true
-- Play cracked sound effect here
task.wait(2)
TweenService:Tween(hatchUI.Frame.Cracked.UIScale,TweenInfo.new(0.5,Enum.EasingStyle.Back,Enum.EasingDirection.Out),{
Scale = 2.7
},true)
-- Play some egg open sound effect here lol
hatchUI.Frame.Egg.UIScale.Scale = 0
hatchUI.Frame.Cracked.UIScale.Scale = 0
hatchUI.Frame.ViewportFrame.Visible = true
local petModel = module3D:Attach3D(hatchUI.Frame.ViewportFrame, chosenPet:Clone())
petModel:SetDepthMultiplier(2)
petModel.Camera.FieldOfView = 60
petModel.Visible = true
task.wait(2)
TweenService:Tween(hatchUI.Frame.ViewportFrame.UIScale,TweenInfo.new(0.25,Enum.EasingStyle.Back,Enum.EasingDirection.Out),{
Scale = 0
},true)
hatchUI:Destroy()
hitBoxDebounce = true
leftArea = nil
gui.Enabled = false
index = 0
pressedHatchBtn = nil
inputted = nil
if plr.Character and plr.Character:FindFirstChild("Humanoid") then
plr.Character:FindFirstChild("Humanoid").WalkSpeed = ogspeed
end
end
end
end
local hatchDebounce = true
pressedHatchBtn = gui.Background.Hatch.TextButton.MouseButton1Click:Connect(function()
if hatchDebounce then
hatchDebounce = false
hatch()
index = 0
hatchDebounce = true
pressedHatchBtn = nil
end
end)
inputted = game:GetService("UserInputService").InputBegan:Connect(function(input,gpe)
if hatchDebounce then
if not gpe then
if input.KeyCode == Enum.KeyCode.E then -- Hatch 1 Egg
hatchDebounce = false
hatch()
index = 0
hatchDebounce = true
inputted = nil
elseif input.KeyCode == Enum.KeyCode.R then -- Hatch 3 eggs
elseif input.KeyCode == Enum.KeyCode.T then -- Auto Hatch
end
end
end
end)
leftArea = openZone.localPlayerExited:Connect(function()
hitBoxDebounce = true
leftArea = nil
gui.Enabled = false
pressedHatchBtn = nil
inputted = nil
index = 0
end)
end
end)
end
end
end