I want to change the SoundService's AmbientReverb property to NoReverb when it was previously Hallway, and Hallway when it was previously reverb. This script works if you remove the else if and just write "else Sound.AmbientReverb = "Hallway"" so it seems as though the if statement never evaluates to true. Can anyone help here?
01 | function onTouch(part) |
02 | local player = game.Players:GetPlayerFromCharacter(part.Parent) |
03 | if player then |
04 | local human = part.Parent:findFirstChild( "Humanoid" ) |
05 | if human then |
06 | Sound = game:GetService( "SoundService" ) |
07 | if Sound.AmbientReverb = = "Hallway" then |
08 | Sound.AmbientReverb = "NoReverb" |
09 | elseif Sound.AmbientReverb = = "NoReverb" then |
10 | Sound.AmbientReverb = "Hallway" |
11 | end |
12 | end |
13 | end |
14 | end |
15 |
16 | script.Parent.Touched:connect(onTouch) |
The if statement fails because Enums (like AmbientReverb) are actually integers, not strings. Setting an Enum using a string is possible, but the reverse isn't true.
Change your if statement to this:
1 | if Sound.AmbientReverb = = Enum.ReverbType.Hallway then |
2 | Sound.AmbientReverb = "NoReverb" |
3 | elseif Sound.AmbientReverb = = Enum.ReverbType.NoReverb" then |
4 | Sound.AmbientReverb = "Hallway" |
5 | end |
Also, you can save a little bit of lag by defining Sound
outside of the function.