Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't AmbientReverb change?

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?


function onTouch(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player then
		local human = part.Parent:findFirstChild("Humanoid") 
		if human then
			Sound = game:GetService("SoundService")
			if Sound.AmbientReverb == "Hallway" then
				Sound.AmbientReverb = "NoReverb"
			elseif Sound.AmbientReverb == "NoReverb" then
				Sound.AmbientReverb = "Hallway"
			end
		end
	end
end

script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

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:

if Sound.AmbientReverb == Enum.ReverbType.Hallway then
	Sound.AmbientReverb = "NoReverb"
elseif Sound.AmbientReverb == Enum.ReverbType.NoReverb" then
	Sound.AmbientReverb = "Hallway"
end

Also, you can save a little bit of lag by defining Sound outside of the function.

4 comments

0

Spotted a quotation mark left out from the previous script's string on line 3. Also, shouldn't line 2 and 4 have "Enum.ReverbType." too, instead of a string?

0

This worked fine, thanks!

1

Spongocardo: (You're right about the quote) When you are setting, most of the Enum values let you set friendly values, and it does the conversion for you. Comparing is where you need to be exact.

0

@BlueTaslem Well, I learn something new everyday. Thanks again.

This discussion is preserved as it was. New answers, comments, and votes are closed.