# Why can´t this boolean stop the sound from playing 60 times a second?

local dontPlayMoreThanOnceDammit = false

function Behavior:Awake()
local sound = CraftStudio.FindAsset( "motorbuson1" )
self.mySoundInstance = sound:CreateInstance()
self.mySoundVolume = 1.0
self.mySoundInstance:SetLoop( true )
self.mySoundInstance:SetVolume( 1.0 )

end

function Behavior:Update()

if dontPlayMoreThanOnceDammit == false and CraftStudio.Input.WasButtonJustPressed( "on" ) then self.mySoundVolume = 1.0 self.mySoundInstance:Play()
dontPlayMoreThanOnceDammit = true
end
if CraftStudio.Input.IsButtonDown( "off" ) then self.mySoundInstance:Stop()
dontPlayMoreThanOnceDammit = false
end end

New contributor
Aloan Moreira is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Did you mean to indent the code under the function Behavior:Update() part? – Vaillancourt 2 days ago
• Also, describing what you're trying to do with the code helps us see the difference between your intention and what you actually ask your program to do. – Vaillancourt 2 days ago
• Awake is preparing what to do.... and on the update, I simply say, if I press the On button, play.. and if I press the off, Stop... so but when the sounds gets played, soon the game crashes giving the error on the picture! so my guess is it is not playing the sound once even with a negating boolean, but 60 times a second.. But so how do I tel it to just play once in the update? – Aloan Moreira 2 days ago
• I'm not fluent in Lua, but, are you missing a coma between self.mySoundVolume = 1.0 and self.mySoundInstance:Play()? – Vaillancourt 2 days ago
• thanks! but no.. Lua, at least in this software does not require commas ;) – Aloan Moreira 2 days ago