Why isn't my sound playing?

Im making Region3 for sounds. I followed Alvin Blox’s tutorial but im now on my own. I wanted to change how the music is played but its not working. Anything helps thanks!

Code [No Sound Is Played The Only Thing That Changes Is The Sounds Time Position]:

local soundRegions = game.Workspace.Regions
local found = false
while wait() do
	for i, v in pairs(soundRegions:GetChildren()) do
		found = false
		local region = Region3.new(v.Position - (v.Size/2), v.Position + (v.Size/2))
		local parts = game.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
		if found then
			--// Play Music
			if script.Sound.IsPlaying == false then 
				script.Sound.SoundId = "rbxassetid://"..v.SoundId.Value
				script.Sound:Play()
				break
			end
		else
			script.Sound:Stop()
		end
	end
end
1 Like

I would recommend to add some prints to check if your sound is even supposed to be played!

4 Likes

This is in a LocalScript, correct?

Ok yes I have done so. And as I can see it spams it seems its being spammed to play.

Maybe its sounds not working? Or its not being loaded?

Its loaded and I have tried what @NORXND_Unname said. I put a print under where the sound is played and it spams the print.

1 Like

Here, try changing line 7 to…

local parts = game.Workspace:FindPartsInRegion3WithWhiteList(
region,
math.huge,
 game.Players.LocalPlayer.Character:GetDescendants()
)

Error: Unable to cast Array to int

Maye try this?

local soundRegions = workspace.Regions
local sound = script.Sound

while wait() do
	for i, v in pairs(soundRegions:GetChildren()) do
		local region = Region3.new(v.Position - (v.Size/2), v.Position + (v.Size/2))
		local parts = workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character,math.huge)
		local found = #parts > 0 and true or false
		
		if found then
			--// Play Music
			if sound.IsPlaying == false then 
				sound.SoundId = "rbxassetid://"..v.SoundId.Value
				sound:Play()
				break
			end
		else
			sound:Stop()
		end
	end
end

You already have it set up so it works with a Whitelist, so just check if the returned table is greater than 0 or not to determine if the player was found in the region

And I think ForeverHDs ZonePlus should be a better way to handle what you need as it works with Region3s, not sure if it works in localscripts as I have never used it

Sorry about that, I accidentally mixed up the order. While fixing this up I noticed that it could be catching an error with the table. By getting all the things in the table you are also getting this like scripts, attachments, etc.
Try this. Not sure if this could be an issue, but it’s worth giving a try.

local WhitelistTable = {}
local CharacterPartsTable =  game.Players.LocalPlayer.Character:GetDescendants()

for i = 1,#CharacterPartsTable do
	if CharacterPartsTable[i]:IsA("BasePart") then
		table.insert(WhitelistTable, CharacterPartsTable[i])
	end
end

local parts = game.Workspace:FindPartsInRegion3WithWhiteList(
	region,
	WhitelistTable,
	math.huge,
)

It still doesn’t work. But thanks for your time. :slight_smile:

Try now? The 2nd parameter automatically handles the descendants so you just have to give it the character instance

And if that still doesn’t work, you may need to do some debugging with pritn statements

This doesn’t work, but thanks.

This error is on line 7: Unable to cast value to Objects

Oh right, forgot it needs to be in a table, do this

local soundRegions = workspace.Regions
local sound = script.Sound

while wait() do
	for i, v in pairs(soundRegions:GetChildren()) do
		local region = Region3.new(v.Position - (v.Size/2), v.Position + (v.Size/2))
		local parts = workspace:FindPartsInRegion3WithWhiteList(region, {game.Players.LocalPlayer.Character}, math.huge)
		local found = #parts > 0 and true or false
		print(parts,found)
		
		if found then
			--// Play Music
			if sound.IsPlaying == false then 
				sound.SoundId = "rbxassetid://"..v.SoundId.Value
				sound:Play()
				break
			end
		else
			sound:Stop()
		end
	end
end

Added print statement for debugging

Is the sound inside workspace? or where is it located at.

Sound somehow only plays inside workspace or in StarterGui.

Its in starter GUI.
(ha ha character limit funny)

Maybe add a repeat wait() until Sound.IsLoaded
or a while not Sound.IsLoaded do wait() end

So it looks like the audio is playing but theres nothing stopping it from playing. So when your in the region it prints PLAYING tons of times. There for the audio not playing.

Could it be that? Maybe it put it somewhere else such as workspace instead?