Pet System Error

I am making a Pet Equip system, but the function is sometimes called 2 times in a way that I don’t know why, which causes it to revert its first job, for example, I click the button while “Equiped” and it becomes “Unequiped”, but although I did not press the button again at this time, the function is called again and this time it does “Equiped” because it is “UnEquiped”, how can I solve it, I am also aware that the code is very long, thank you for reading the code so long. I’m also sorry that I don’t have good english :pray: :pray:

game.Players.PlayerAdded:Connect(function(player) 
local debounce = false
    local screenGui = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui") 
    local ScrollingFrame = screenGui:WaitForChild("CrownUI",100):WaitForChild("ScrollingFrame",10)
    
    local function Pet()
        if #ScrollingFrame:GetChildren() > 1 then
            for i, PetLabel in ipairs(ScrollingFrame:GetChildren()) do
                if PetLabel.Name ~= "UIGridLayout" then
                    PetLabel.ImageButton.MouseButton1Down:Connect(function()
                        if debounce == false then
                            if player[PetLabel.Name].Value > 0 then
                                local Character = player.Character
                                local PetSlot = Character:WaitForChild("PetSlot")
                                if PetLabel.ImageButton.Value.Value == "UnEquiped" then
                                    debounce = true
                                    PetLabel.ImageButton.Value.Value = "Equiped"
                                    player.Equiped.Value += 1
                                    PetLabel.ImageButton.TextLabel.Text = "+"
                                    local clone = game.ReplicatedStorage.Pets[PetLabel.Name]:Clone()
                                    local Slot
                                    if #PetSlot:WaitForChild("Slot1"):GetChildren() == 0 then
                                        Slot = 1
                                    elseif #PetSlot:WaitForChild("Slot2"):GetChildren() == 0 then
                                        Slot = 2
                                    elseif #PetSlot:WaitForChild("Slot3"):GetChildren() == 0 then
                                        Slot = 3
                                    elseif #PetSlot:WaitForChild("Slot4"):GetChildren() == 0 then
                                        Slot = 4
                                    else
                                        print("Full")
                                        wait()
                                        PetLabel.ImageButton.Value.Value = "UnEquiped"
                                        PetLabel.ImageButton.TextLabel.Text = ""
                                        return
                                    end
                                    print("Equiped")
                                    clone.Parent = PetSlot:WaitForChild("Slot"..Slot)
                                    clone.Crown.CFrame = PetSlot:WaitForChild("Slot"..Slot).CFrame
                                    player.PetMulti.Value += clone.Multi.Value
                                    local WeldConstraint = Instance.new("WeldConstraint")
                                    WeldConstraint.Part0 = clone.Crown
                                    WeldConstraint.Part1 = PetSlot:WaitForChild("Slot"..Slot)
                                    WeldConstraint.Parent = clone.Crown
                                    debounce = false
                                    return 
                                elseif PetLabel.ImageButton.Value.Value == "Equiped" then
                                    debounce = true
                                    PetLabel.ImageButton.Value.Value = "UnEquiped"
                                    player.Equiped.Value -= 1
                                    if PetSlot:WaitForChild("Slot1"):FindFirstChild(PetLabel.Name) then
                                        PetSlot:WaitForChild("Slot1"):FindFirstChild(PetLabel.Name):Destroy()
                                        print("1")
                                    elseif PetSlot:WaitForChild("Slot2"):FindFirstChild(PetLabel.Name) then
                                        print("2")
                                        PetSlot:WaitForChild("Slot2"):FindFirstChild(PetLabel.Name):Destroy()
                                    elseif PetSlot:WaitForChild("Slot3"):FindFirstChild(PetLabel.Name) then
                                        print("3")
                                        PetSlot:WaitForChild("Slot3"):FindFirstChild(PetLabel.Name):Destroy()
                                    elseif PetSlot:WaitForChild("Slot4"):FindFirstChild(PetLabel.Name) then
                                        print("4")
                                        PetSlot:WaitForChild("Slot4"):FindFirstChild(PetLabel.Name):Destroy()
                                    else
                                        print("Else")
                                        print(PetLabel.Name)
                                    end
                                    local clone = game.ReplicatedStorage.Pets[PetLabel.Name]:Clone()
                                    player.PetMulti.Value -= clone.Multi.Value
                                    PetLabel.ImageButton.TextLabel.Text = ""
                                    print("UnEquiped")
                                    debounce = false
                                end
                            end
                        end
                    end)
                end
            end
        end
    end 
    Pet()
    ScrollingFrame.ChildAdded:Connect(function()
        Pet()
    end)
end)

Your code is hard to read on mobile. The issue is probably that with every script you
create you create a new connection thingy.

1 Like