Title says it all, I am trying to setup sound regions and make it so that the sounds fade in or out realistically, however this has proven to be a real pain.
Basically, sometimes when I launch the game the script works as intended, other times though, it'll throw this error and it won't work.
Here is the code in question.
-- [ TODO: FIX LAG ] -- -- [ stops and starts regional sounds with fading in and out ] -- -- [ DO NOT MAKE A SOUND WITH ANY VOLUME OTHER THAN 0, THIS SCRIPT WILL MANAGE THAT ] -- get sound service local sounds = game:GetService("SoundService") -- get sound regions (area where sounds are played) local sound_regions = workspace.SpecialBrushes:WaitForChild("SoundRegions") -- detect if player is in zone local found = false local player = game.Players.LocalPlayer while wait() do -- do this for each individual region for i, v in pairs(sound_regions:GetChildren()) do found = false -- create region from 2 points local region = Region3.new(v.Position - (v.Size/2), v.Position + (v.Size/2)) -- find parts of user that are in region local parts = workspace:FindPartsInRegion3WithWhiteList(region, player.Character:GetDescendants()) -- go through everything in parts table for _, part in pairs(parts) do if part:FindFirstAncestor(player.Name) then found = true break else found = false end end -- play audio, target audio is 0.3 if found == true then if sounds.Ambience[v.Name].isPlaying == false then sounds.Ambience[v.Name]:Play() -- fade out for count = 0, 300, 1 do sounds.Ambience[v.Name].Volume += 0.001 wait(0.0001) end break end else if sounds.Ambience[v.Name].isPlaying == true then -- fade in for count = 0, 300, 1 do sounds.Ambience[v.Name].Volume -= 0.001 wait(0.0001) end sounds.Ambience[v.Name]:Stop() end end end end
Help would be much appreciated, and I apologize in advance for any newbie errors I may have made as it's been a while since I scripted on ROBLOX.
EDIT: ANSWER BELOW
Thanks to T3_MasterGamer I found out that no, you should not put something like this in the StarterPlayerScripts, and that a more appropriate place to put it would be in StarterGui or StarterPack, I myself chose StarterPack as I feel like that fits it more, but you could also choose StarterGui.