Anyway to shorten this script without it breaking anything?

Hey I was trying to shorten this script but all my attempts to trying to keeps breaking the sound. This script belongs to a siren system.

Here’s the script I’ve been trying to reduce the number of if statements on it.

s = script.Parent
slowness = script.Slowness
slowness2 = script.Slowness2
slowness3 = script.Slowness3


while true do
pitch = s.Pitch
 
if s.Pitch >= 0.99 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.98 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.97 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.96 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.95 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.94 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.93 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.92 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.91 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.9 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.89 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.88 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.87 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.86 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.85 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.84 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.83 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.82 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.81 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.8 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.78 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.76 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.74 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.72 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.7 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.68 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.66 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.64 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.62 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.6 then
s.Pitch = s.Pitch- slowness.Value
else
end


if s.Pitch >= 0.57 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.54 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.51 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.48 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.45 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.42 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.38 then
s.Pitch =s.Pitch - slowness.Value
else
end

if s.Pitch >= 0.34 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.3 then
s.Pitch = s.Pitch- slowness.Value
else
end

if s.Pitch >= 0.25 then
s.Pitch = s.Pitch- slowness2.Value
else
end

if s.Pitch >= 0.2 then
s.Pitch = s.Pitch- slowness2.Value
else
end

if s.Pitch >= 0.15 then
s.Pitch = s.Pitch- slowness2.Value
else
end

if s.Pitch >= 0.1 then
s.Pitch = s.Pitch- slowness3.Value
else
end

if s.Pitch >= 0.05 then
s.Pitch = s.Pitch- slowness3.Value
else
end

if s.Pitch >= 0 then
s.Pitch = s.Pitch- slowness3.Value

else
end
wait()
end

Here is what I tried so far:

s = script.Parent
pitchSlownessTable = {
    {0.99, script.Slowness.Value},
    {0.95, script.Slowness.Value},
    {0.85, script.Slowness.Value},
    {0.7, script.Slowness.Value},
    {0.25, script.Slowness2.Value},
    {0.1, script.Slowness3.Value},
    {0, script.Slowness3.Value}
}

while true do
    pitch = s.Pitch
    for i = 1, #pitchSlownessTable do
        if pitch >= pitchSlownessTable[i][1] then
            s.Pitch = s.Pitch - pitchSlownessTable[i][2]
            break
        end
    end
    wait()
end

But it keeps breaking the sound pitch like in the video below. Its suppose to be going down not staying the same. Can somehow make the sound work without using all the if statements?
Sorry about the bad sound quality
robloxapp-20230312-1606075.wmv (1.8 MB)

for the First one, yes:

while true do
    task.wait(.1)
    pitchHolder = s
    pitchHolder.Pitch -= slowness.Value
end

The issue seems to be that you are only checking if the pitch is higher than the value in the table, but you are not checking that it is lower than the previous value.

The first script does that to, but due to way it is written, the pitch will be overwritten later with proper value anyway (since there are no break or continue statements between the if else conditions)

Finally, this may not be a problem, but in the second script You are hard coding Slowness values into the table, rather than number values references. This will work, but will not react if you change those Slowness values later in the game via player actions or other scripts.

I will provide a solution now. Note that for the first pitch check, you are only checking one condition, as there is no former entries in the table.

s = script.Parent
pitchSlownessTable = {
    {0.99, script.Slowness}, -- by storing a reference, rather than value, script will react to possible changes in slowness during the game
    {0.95, script.Slowness},
    {0.85, script.Slowness},
    {0.7, script.Slowness},
    {0.25, script.Slowness2},
    {0.1, script.Slowness3},
    {0, script.Slowness3}
}

while true do
    pitch = s.Pitch
    for i = 1, #pitchSlownessTable do
        if (i > 1 and pitch >= pitchSlownessTable[i][1]
               and pitch < pitchSlownessTable[i-1][1]) or
               (i == 1 and pitch >= pitchSlownessTable[i][1]) then

            s.Pitch = s.Pitch - pitchSlownessTable[i][2].Value -- retrieve value here
            break

       end
         
    end
    wait()
end

EDIT: I edited the code as I realized it was supposed to be other way round (pitches are descending in the table)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.