UI Button gets activated multiple times?

Greetings, I have a UI built that works as a shop that has a couple of buttons. All of them work well besides one that simply just runs as many times as it wants. The more time I stay inside the game, the more events it will send once I click it. The other buttons have no issue and work as they should, closing the UI, and opening other tabs. This is incredibly weird and I wasn’t able to find any fix, please help!

#This is outside at the top of the script:
UpgradeButton : button_loud = button_loud {}
TextForUpgradeWeapon<localizes> : message = "FINISH WEAPON"

#This is in the function that makes the canvas:
UpgradeButton.OnClick().Subscribe(WeaponUpgradeButtonClickEvent)


UpgradeButton.SetText(TextForUpgradeWeapon)

#This is the function that triggers when the button is clicked:
WeaponUpgradeButtonClickEvent(Wdata:widget_message):void=
        Print("Player Pressed UpgradeButton, Start Event") #This gets printed as many times as it wants
        if(Player:=Wdata.Player, PlayerUI:=GetPlayerUI[Player], SelectedButton:=Wdata.Source):
            if(Widgets:=MaybeMyUIPerPlayer[Player]?):
                 Print("Okay") #this too

There are no errors btw
Thank you!

Bump, I don’t know what I did wrong, please help!

did you ever find a fix to it?

@DoomTheDev @Tofuszn The issue was because the UpgradeButton was declared globally at the top of the script meaning each time you add it to a player it would always be the same button instead of a new one generated for that player, the problem comes that you are “subscribing” the button clicked event when you attach it to a player UpgradeButton.OnClick().Subscribe(WeaponUpgradeButtonClickEvent) And never unsubscribe from it, which means each time you subscribe you are just increasing by one the amount of times the function will be called when the button is pressed, hope that makes sense. If you need a further explanation or questions let me know.

1 Like

That makes perfect sense! I had my text in a global scope after taking out of a class to experiment with it. I put it back into a class and now it works perfectly :grin: thank you so much!

1 Like