So I was working on a horror game, and I wanted to reuse a sound region script I found in a tutorial. I updated it, made it a lot better, and it worked, until it didn’t. I didn’t open the script, but it broke. It always thinks the player isn’t in the region3.
Local script, inside of StartGui:
--// Sound region handler originally made by AlvinBlox
--// Updated by Babybunnyiscute19
local TweenS = game:GetService("TweenService")
local soundRegionsWorkspace = workspace:WaitForChild("SoundRegions")
local found = false
while task.wait() do
for i, zone in pairs(soundRegionsWorkspace:GetChildren()) do
found = false
local region = Region3.new(zone.Position - (zone.Size / 2), zone.Position + (zone.Size / 2))
local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
for _, part in pairs(parts) do
if part:FindFirstAncestor(game:GetService("Players").Name) then
found = true
break
else
found = false
end
end
local audio = script.Parent.SoundRegions:WaitForChild(zone.Name)
local configs = require(audio.Configs)
if found == true then
if audio.IsPlaying == false then
audio:Play()
print("in")
TweenS:Create(audio, TweenInfo.new(configs.tweenInTime), { Volume = configs.volume }):Play()
task.wait(configs.tweenInTime)
break
end
else
print("out")
TweenS:Create(audio, TweenInfo.new(configs.tweenOutTime), { Volume = 0 }):Play()
task.wait(configs.tweenOutTime)
audio:Stop()
end
end
end
Any help is appreciated!
If you need a sound to only play in a certain proximity then use the following property shared by all “Sound” instances.
https://developer.roblox.com/en-us/api-reference/property/Sound/RollOffMaxDistance
2 Likes
The script and the folder with the sounds are in starter gui which means they play everywhere no matter where you are on the map. I also saw that the audios aren’t playing at all even when I’m in the region. The script doesn’t detect when I’m in a region for some reason.
So I went back to the original script, then I reupdated the code and it worked, I have no clue why, but It’s probably me doing ctrl + h on something I shouldn’t have done.
Ugly stupid dumb code:
--// Sound region handler originally made by AlvinBlox
--// Updated by Babybunnyiscute19
local TweenS = game:GetService("TweenService")
local soundRegionsWorkspace = workspace:WaitForChild("SoundRegions")
local found = false
while task.wait() do
for _, zone in pairs(soundRegionsWorkspace:GetChildren()) do
found = false
local region = Region3.new(zone.Position - (zone.Size/2),zone.Position + (zone.Size/2))
local parts = workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
for _, part in pairs(parts) do
if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
found = true
break
else
found = false
end
end
local audio = script.Parent.SoundRegions:WaitForChild(zone.Name)
local configs = require(audio.Configs)
if found == true then
if audio.IsPlaying == false then
audio:Play()
TweenS:Create(audio, TweenInfo.new(configs.tweenInTime), { Volume = configs.volume }):Play()
task.wait(configs.tweenInTime)
break
end
else
TweenS:Create(audio, TweenInfo.new(configs.tweenOutTime), { Volume = 0 }):Play()
task.wait(configs.tweenOutTime)
audio:Stop()
end
end
end
2 Likes
(Sorry to bump this but this is not “Ugly stupid dumb code”, it’s definitely readable. I would recommend to stop using pairs anymore however this is from 2021 so…)
I’ve never heard anyone say stop using pairs, what other ways, and improvements, are there?
You can loop through stuff without ipairs and pairs. I personally don’t like it because it makes the code longer.
For example:
for _, v in soundRegionsWorkspace:GetChildren() do
It acts as the same thing.
Edit: I realized that this reply was misleading. Pairs shouldn’t be often used in Luau because it’s automatically done by Roblox itself. However, for programs like Lua it will be very important. ipairs and pairs still have use cases too! Just to keep stuff shortened.