Audio randomly cutting in and out

Hello, could someone help me with a sound script I have made?

  1. What do you want to achieve? I am trying to make a sound that only plays when a player is in a certain area.

  2. What is the issue? The sound script will randomly cut out and come back in after a few seconds, seemingly at partially consistent parts in the song

  3. What solutions have you tried so far? I have tried to determine why this isn’t working, but I can’t seem to find any breaks. When parenting the audio to the workspace, it plays regularly with no cuts, suggesting there is something wrong with the script. However, the script consistently prints the correct play time even when the audio is cut for several seconds, suggesting there is something wrong with the audio.

Sound script:

local sound = script.Parent.AstralTheme --Change name
local db = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and db == false then
		db = true
		wait(0.5)
		sound:Play()
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	sound.Playing = false
	wait(0.5)
	db = false
end)

while true do
	wait(1)
	print(tostring(sound.TimePosition))
end

Thanks for reading this post!

6 Likes

TouchEnded is for sure not working the way you want it to work. What are you trying to do here?

3 Likes

I’m trying to make it so that when the player touches the area (and is in the area), the music will start playing, and when they leave, the music stops.

2 Likes

So music based on zones. Touch is not designed for that. Can I instead recommend the ZonePlus module?

3 Likes

Idk how to use modulescripts and plugins lol

3 Likes

There are tutorials online which should help you learn. TouchEnded really isn’t the method you want for this type of work

3 Likes

Do u know if it’s an issue with the touch and touchended or no?

3 Likes

The TouchEnded fires when it still has the character inside.

3 Likes

Wait why is the timeposition printing correctly then?

2 Likes

I don’t know, you’re printing it every second. Many actions could happen in that second

2 Likes

For some reason the audio is still cutting out even with the module?

3 Likes

It’s because a foot can touch it and then immediately leave when it’s lifted up.

Maybe add this into it

local start

.Touched:Connect(function()
   start = tick()
end)

.TouchEnded:Connect(function()
   if tick() - start < 1 then return end--Change the number and play around with it.


end)


2 Likes

It sounds like for what you intend to do, Touched is not gonna work. I would recommend looking at Region3 in order to take on this task. There are some tutorials online which you can use to help as well. As others have said in this post, the problem with Touched and TouchEnded though is that it fires every time your player moves, since the character is repeatedly touching and not touching the part.

Also a note: endless loops with other functions (that don’t have calls nested within the while loop) in a script can cause issues with the code, since it will be forever stuck on the loop.

while true do
	wait(1)
	print(tostring(sound.TimePosition))
end
2 Likes
local sound = script.Parent.AstralTheme --Change name
local db = false

script.Parent.Touched:Connect(function(d)
end)

while wait() do
	local plris = false
	for i,v in pairs(script.Parent:GetTouchingParts()) do
		if v and v.Parent and v.Parent:FindFirstChild("Humanoid") then
			plris = true			
			break
		end
	end
	if plris then
		if db == false then
			db = true
			sound:Play()
		end
	else
		sound:Stop()
		db = false
	end

	--print(tostring(sound.TimePosition))
end

tried this and it worked
hope it works for you
you might need to change some stuff

2 Likes

Hello!

You could also try using SoundService:SetListener(). SetListener is bascially used to change the source from which you hear sounds. So let’s say there’s a part in workspace that’s playing music.
The sound will get less audible as the part gets farther from the player, and louder the closer they get to it.

If you’re using headphones, the sound will shift to ear to ear depending on the position and CFrame of the MusicPart.

You can do game.SoundService:SetListener(Enum.ListenerType.Camera) for default behavior.

Here is the documentation page if you want to learn more:
SoundService | Documentation - Roblox Creator Hub

This script should work for you:

--Local Script parented to your part
game:GetService("SoundService"):SetListener(Enum.ListenerType.ObjectCFrame, workspace.YourPart) --Change to the correct name.

Hope this helps! :slight_smile:

2 Likes

Could u please explain what the d in the .Touched function does, and if the script after the 7th line should be in the .Touched event?

2 Likes

Touched with stuff inside of it forces the GetTouchingParts() function always return 0, so we need to put nothing inside of it
(d was a test)

1 Like

MaxDistance and MinDistance didn’t work either so im beginning to think the audio’s corrupted or something (nvm it’s the script other audios are also broken)

1 Like

Did you try my solution?

[char limit]

1 Like