Dealman Posted September 12, 2013 Share Posted September 12, 2013 I don't know if I'm doing something wrong, but it seems like setSoundVolume is slightly inaccurate and as such makes it unnecessarily complex to make use of. For example: Code Example 1; local currentVolume = 1 function changeMapMusicVolume(pressedButton, keyState) if((pressedButton == "num_add") and (keyState == true)) then currentVolume = tonumber(currentVolume+0.1) setSoundVolume(mapMusic, currentVolume) outputChatBox(tostring(currentVolume)) if(currentVolume >= 1) then outputChatBox("#696969[Map]: #FFD700You've reached maximum volume!", 255, 255, 255, true) end elseif((pressedButton == "num_sub") and (keyState == true)) then currentVolume = tonumber(currentVolume-0.1) setSoundVolume(mapMusic, currentVolume) outputChatBox(tostring(currentVolume)) if(currentvolume <= 0) then outputChatBox("#696969[Map]: #FFD700You've muted the music!", 255, 255, 255, true) end end end addEventHandler("onClientKey", getRootElement(), changeMapMusicVolume) There I'm trying to make a simple script where as you can increase and decrease the music by increments of 0.1. And if it reaches 1 or above, it will tell you that you've reached the maximum volume. And the same if you reach 0 or below. Keep in mind the wiki says that it only accepts a value between 0.0 and 1.0, yet it still goes above and below without any effect. However, whenever I try to subtract it, it will return this error - "Attempt to compare nil with number". It will still decrease the volume, though. This is what it actually returns when subtracted; 1 0.89999997615814 0.79999995231628 0.69999992847443 0.59999990463257 0.49999991059303 0.3999999165535 0.29999992251396 0.19999992847443 0.09999992698431 -7.3015691270939e-008 -0.10000007599592 It never outputs the message that the sound was muted, and I can still hear the music slightly when it reaches -7(Which is actually -0.000000007) and -0.1. It seems like it still reads -0.1 as 0.1 or it for some reason gets stuck at 0.1 but keeps returning that it's below that...? In order to get rid of the error, I need to change the expression in "if(currentvolume <= 0) then" to either equal to 0 or less than or equal to -0. For example - "if(currentVolume <= -0". While that gets rid of the error that is being output, it doesn't actually fix the issue in itself. In order to fix that, I had to add this bit of code; currentVolume = (tonumber(currentVolume) <= 0 and 0) or (tonumber(currentVolume) >= 1 and 1) or tonumber(currentVolume) So finally the complete and working code would look like this; function changeMapMusicVolume(pressedButton, keyState) if((pressedButton == "num_add") and (keyState == true)) then currentVolume = ((currentVolume)+0.1) currentVolume = (tonumber(currentVolume) <= 0 and 0) or (tonumber(currentVolume) >= 1 and 1) or tonumber(currentVolume) setSoundVolume(mapMusic, currentVolume) if(currentVolume >= 1) then currentVolume = 1 outputChatBox("#696969[Map]: #FFD700You've reached maximum volume!", 255, 255, 255, true) end elseif((pressedButton == "num_sub") and (keyState == true)) then currentVolume = ((currentVolume)-0.1) currentVolume = (tonumber(currentVolume) <= 0 and 0) or (tonumber(currentVolume) >= 1 and 1) or tonumber(currentVolume) setSoundVolume(mapMusic, currentVolume) if(currentVolume <= -0) then setSoundVolume(mapMusic, 0) currentVolume = 0 outputChatBox("#696969[Map]: #FFD700You've muted the music!", 255, 255, 255, true) end end end addEventHandler("onClientKey", getRootElement(), changeMapMusicVolume) I don't know if this is a side-effect of the whole Client being less accurate than the server in terms of math or if setSoundVolume have bad calculations itself, but it seems rather silly if we have to make a workaround such as above to fix this...? Link to comment
Recommended Posts