Hello, could someone help me with a sound script I have made?
What do you want to achieve? I am trying to make a sound that only plays when a player is in a certain area.
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
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
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.
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)
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
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
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.
--Local Script parented to your part
game:GetService("SoundService"):SetListener(Enum.ListenerType.ObjectCFrame, workspace.YourPart) --Change to the correct name.
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)