Sound Wont play inside of a specific Region (Bug/Idk why its happening)

I have a script that plays the sound inside the Part if the player is touching it, that is located inside the Folder Regions inside Workspace…

My problem is if the Player spawns too far away from the Regions or the Region Part, it won’t play the Sound anymore that is inside the part… I have no idea what might be the problem… I would appreciate the help!

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character
plr.CharacterAdded:Connect(function()
	char=plr.Character
end)

local rs = game:GetService("RunService")
local ts = game:GetService("TweenService")

local info = TweenInfo.new(0.5,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0)

local regions = {}

for i,v in pairs(workspace.Regions:GetChildren()) do
	tab = {
		["CFrame"] = v.CFrame,
		["Sound"] = v.Sound,
		["Active"] = false,
		["Size"] = v.Size,
		["Volume"] = v.Sound.Volume
	}
	tab.Sound.Looped=true
	tab.Sound.Volume=0
	tab.Sound:Play()
	tab.Sound.Parent=game.SoundService
	table.insert(regions,tab)
end
workspace.Regions:Destroy()

function fade(sound,vol)
	local goal = {}
	goal.Volume= vol
	local tween = ts:Create(sound,info,goal)
	tween:Play()
end

function checkisinregion()
	for i,v in pairs(regions) do
		local ovlapparams = OverlapParams.new()
		ovlapparams.FilterType= Enum.RaycastFilterType.Whitelist
		ovlapparams.FilterDescendantsInstances= {char}
		local found = #workspace:GetPartBoundsInBox(v.CFrame,v.Size,ovlapparams)>0 and true or false
		if found then
			if not v.Active then
				v.Active=true
				fade(v.Sound,v.Volume)
			end
		else
			if v.Active then
				v.Active=false
				fade(v.Sound,0)
			end
		end
	end
end

rs.RenderStepped:Connect(function()
	checkisinregion()
end)

1 Like

You don’t need to do anything with :GetPartsInBound() or anything in your script. Parent the sound object to the part, as you get further away the sound will get harder to listen to. If you want the sound to be heard at by a certain distance there’s a property for it here’s the link:
https://create.roblox.com/docs/reference/engine/classes/Sound#RollOffMaxDistance

sound.RollOffMaxDistance

By the way if you wanna know the distance you could get the distance between two vector3 positions like part positions and then use magnitude. Example:

(part1.Position - part2.Position).Magnitude -- returns the distance between the parts in studs (number)

Oh yeah and you can just keep those parts invisible. (Or you can just test out a bunch of random numbers and find out which one appeals to you by making an accurate guess)
Edit: You can also use minimum distance which will control how close the player needs to be to hear it to begin with, maximum distance controls how far away they can go before no longer hearing it.

2 Likes

This is not exactly what I want… I want it that if the Player is in a Region it plays the Sound and if leaves the Region It Stops (fades away) the sound.

Example:
1.Player Enters region_1 (it plays the sound.)
2.Player Leaves the region_1 (it stops the sound.)

My problem is that if the player spawns to far away from the region and the player goes to the region it wont play the sound…

Working: (when spawn near it)

Not Working: (when spawn far away from it

1 Like

But it is automatically within a region if you use the property. No matter which position you are if you are a certain amount of studs away from the sound on any side it won’t be heard. Try it out and see what I mean.

2 Likes

Here’s an easy fix though if you still wanna do it in a region for some reason. Parent the local script to the startercharacterscripts, this would mean that this would work even after the character respawns.

while true do
task.wait()
local overlapparams = OverlapParams.new()
overlapparams.FilterType= Enum.RaycastFilterType.Whitelist
overlapparams.FilterDescendantsInstances= {script.Parent} -- parent of the script is the character if parented to startercharacterscripts
local region = workspace:GetPartsInBound(part.CFrame, part.Size, overlapparams)
if #region == 1 then
sound:Play()
else
sound;Stop()
end

obviously tween the fading out or something, but this should be similiar.

1 Like