Sound plays multiple times when in main hand

Started by JrBacon on

Topic category: Help with modding (Java Edition)

Last seen on 01:39, 15. Feb 2024
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Sound plays multiple times when in main hand

Hi, I am trying to make it so that when you select an item in offhand or in main hand, it just plays a noise and gives you the effect speed 1. But when i do this, it plays constantly. I know why this happens, but how do you make the sound only play once. Thank you!!

Last seen on 21:55, 20. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Try making a player-lifetime…
Thu, 11/02/2023 - 03:28

Try making a player-lifetime logic variable, (I'll call it 'sound_played'). When the sound plays, in addition to playing the sound, set this variable to true. Then, nest the code that's playing the sound in an if bracket that first checks if the variable is true. Lastly, on player update tick, if the player isn't holding the item in their main hand, set 'sound_played' back to false.

Essentially, you just need a variable to tell the game whether or not the sound's already been played, and you can then flip it back and forth as needed.