Code running +1 time every time

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

maybe it’s because you didn’t put a task.wait() before setting the debounce to true

nah, that ain’t it lol, I’ve tried that too
edit: i fixed it

It looks like the code for incrementing the index variable is inside the localPlayerEntered event. This means that every time a player enters the openZone , the index variable is incremented by 1.

This is why the index variable increases by more than 1 each time you run the code. To fix this issue, you can move the code that increments the index variable outside of the localPlayerEntered event, so that it is only incremented once each time the code runs.

local isHatching = true
local index = 0
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
      openZone.localPlayerEntered:Connect(function()
        warn("entered")
        if hitBoxDebounce and plr.Character then
          hitBoxDebounce = false
          index += 1
          print(index)
          -- Rest of your code here
        end
      end)
    end
  end
end

By moving the declaration of the index variable outside of the openZone.localPlayerEntered event, the value of the index variable will be preserved between GUI opens, and the pet count will no longer increase every time the GUI is opened.

no that’s not it either, the index thing was just for me to test smth

You have to disconnect events whenever the egg is hatched. Your code creates new events when the player gets into the hitbox (i guess). So you must disconnect the events when the player hatches

local event = openZone.localPlayerEntered:Connect(function()
print(“Do something”)
end)

event:Disconnect()

I think this should work, you have to apply it to every event

1 Like

If you would look at the script, I actually did that, didn’t work

I’m looking at the script right now and I don’t see it. You connect four events but I don’t see any disconnects anywhere.

1 Like

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