Confusing title but, its late at night (and the title cant fit it). bare with me.
So I have a MagnitudeTable and a SoundVolumeTable. A friend of mine did a longer, elseif way of doing what im trying to do and I wanna figure out if its possible to make the script shorter by developing a sort of system to automatically check if a person is this many blocks away, and if it is it lowers the volume, without a lot of elseif statements.
I can see that the magnitude would do a constant +2, and the Volume would do a constant -.10. equally; a constant; until the volume hits 0. The MagnitudeTable and SoundVolumeTable was an attempt to figure out if i can make it so that I dont have to make so many elseif statements to do what im trying to do.
Would it be possible to make a system in lua of what im trying to do? Below is a sad attempt of two lines of code I tried to figure out to do it my own before I went to this website.
-- this is a local script, a small snip of it relating to the question. local MagnitudeTable = {5,7,9,11,13,15,17,19,21,23,25,27} local SoundVolumeTable = {1,.90,.80,.70,.60,.50,.40,.30,.20,.10,0}
TL;DR: how do i make a system which where it automatically checks the magnitude away from the part and user, and if its less or equal to a number in the magnitude table then it automatically sets the volume of the sound to a number equivalent to the number on the magnitude table? (EXAMPLE: Magnitude of Torso - Part position is less than or equal to 9, sound volume is set to .80)
the other parts of the script work, its just this that'll basically make the script actually work.
Also, yes I know a sound emitter exists on roblox. But that isn't what I want.
So correct me if I'm wrong but you want volume to be negativley correlated to the magnitude. I don't think using a table would be the best approach to do this, when you could instead do some simple linear math. Since for each time magnitude increases by 1 the sound volume decreases by .05 the slope of the equation is -.05. Using some more linear math we can derive an equation which is y = 1.25 - .05x In lua:
local SoundVolume = 1.25 - (.05 * Magnitude)
I'm assuming you want to clamp the volume between 1 and 0 so it isn't too loud or negative, in which case you can use math.clamp or and and or statements:
local SoundVolume = math.clamp(1.25 - (.05 * Magnitude),0,1)