How to wait until a property has loaded

I added a feature to mute soundfx in my game. To do this I set the volume of the sound group to 0. This works most the time, however when I start up studio and switch it immediately I get and error. The soundgroup is not nil, yet I cant access the volume. The commented out line is my code that errored initially. The code below it always warns when I attempt to access the soundgroups volume.

--game.SoundService.SoundFx.Volume = slider:GetAttribute("IsEnabled") and 1 or 0
					
local soundFx = game.SoundService:WaitForChild("SoundFx")
if soundFx.Volume then
	soundFx.Volume = slider:GetAttribute("IsEnabled") and 1 or 0
else
	warn("Volume property is missing from SoundFx") -- Always when I access volume fast
end

Its very frustrating because it only errors occasionally I have determined 100% that the code above the problem. Any ideas on how I could fix this?

1 Like

You should be fine with just doing:

local soundFx = game:GetService("SoundService"):WaitForChild("SoundFx")
soundFx.Volume = slider:GetAttribute("IsEnabled") and 1 or 0

Using the WaitForChild already ensures that the Sound object has already fully loaded, which then allows you to change the properties of it and such.

2 Likes

Ok I have tested this and I still am encountering the same error message:
image

Edit: I found the solution I, had another folder named the same thing as the group facepalm @xRo_Fishy 's solution makes the code cleaner so Im marking it

2 Likes

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