Sound sometimes doesn't play when using :Play()

so, i have a timer that whenever the time goes down, it makes a sound, but sometimes the sound doesnt really play correctly, i made a video so yall can see what i mean:

video (youtube link)

this isnt the actual game so thats why there isnt really a timer, here is the code:

if not sound.IsLoaded then
	sound.Loaded:Wait()
end
while task.wait(.5) do
	sound:Play()
end

thanks! :smiley:

6 Likes

It usually means the sound hasnā€™t fully loaded/loaded correctly.

This post of mine should help:

In the post I talk about loading animations but it works the same for sounds as well.

4 Likes

okay, so i need to call ContentProvider and just do ContentProvider:PreloadAsync({sound}) right?

i also forgot that the script is in local script so idk if content provider works on local scripts

thanks :smiley:

edit: i forgot to put the sound in a table, i just noticed

2 Likes

i did all the things u said in that post, but im still getting the same result shown in the video :confused:

this is how the script looks after i did the changes:

local ContentProvider = game:GetService("ContentProvider")

ContentProvider:PreloadAsync({sound})

while task.wait(.5) do
	if not sound.IsLoaded then
		sound.Loaded:Wait()
	end
	sound:Play()
end

again, the code is in a local script, so i dont know if that has something to do with it

thanks :smiley:

2 Likes

May I see how youā€™re defining the sound variable?

the sound is on workspace so

local sound = workspace.Sound
1 Like

Try using :WaitForChild() so the object has time to load in.

local sound = workspace:WaitForChild("Sound")
2 Likes

i made the changes and its just the same thing, the sound sometimes doesnt play correctly

Why are you doing all this waiting and sound loading within the loop?
The sound should be loaded into the workspace when the game is loaded, way before your timer is even activated.
In your timer script reference the workspace.sound at the beginning with a WaitForChild, then just play it when the timer runs out.

while task.wait(.5) do 
    sound:Play()
end

will just keep starting the sound every half second.

if timer == 0 then
    sound:Play()
end

will work when the timer hits 0, and only play the sound 1 time.

1 Like

I dont see the point of this comment.

I didnt say i wanted the sound to play when the timer hits 0, i want the sound to play when the times changes. The code i provided is just a test, but it is basically how my main script works, it plays when the timer changes

also all the loading stuff is just based on the replies that iā€™ve got in this post

thanks :smiley:

Ah, sorry, I misunderstood.
How long is the sound? If it takes longer than 1/2 a second is the sound not being heard for that reason?

I wonder if this would this work better?

while (a bool variable indicating your timer is decreasing) do
    sound:Play()
    task.wait(.5)
    sound:Stop()
end
2 Likes

im sure the sound doesnt have more than .5 seconds of duration, so i dont think the problem is related to that.

and so im going to try that code rn. (edit: sadly doesnt change anything)

thanks :smiley:

3 Likes

This is still a problem for me, any ideas?

Did you read through all the posts above?

Is your sound in a Part or Attachment, or the workspace?

I did, yes

A part, but Iā€™ve tried it in an attachment and workspace as well. Iā€™ve created a repro example to demonstrate this is a problem.

The following bug repro server script works without much setup needed. To test, run any experience and insert anything into the workspace camera and observe the sound behavior

local function twinklemaker()
	local starsound = Instance.new("Sound")
	starsound.SoundId = "rbxassetid://9060788686"
	starsound.RollOffMaxDistance = 2048
	starsound.RollOffMinDistance = 10
	starsound.RollOffMode = Enum.RollOffMode.Linear
	starsound.Volume = 0.04
	return starsound
end
local function bugtest()
	local part = Instance.new("Part")
	part:PivotTo(workspace.Camera.CFrame)
	part.Anchored = true
	part.Parent = workspace.Camera
	workspace.Camera.ChildAdded:Connect(function()
		local folder = Instance.new("Folder")
		folder.Name = "*"
		folder.Parent = workspace
		local testsound = twinklemaker()
		--testsound.SoundId = "rbxassetid://9060788686"
		testsound.Parent = workspace.Camera.Part
		local first = coroutine.create(function()
			testsound:Play()
		end)
		coroutine.resume(first) -- comment out this line and you'll experience no problems!
		local sounds = {}
		local testcount = 7
		while testcount > 1 do
			task.wait(1)
			testcount -= 1
			local clone = testsound:Clone()
			clone.Name = "sound"..testcount
			clone.Volume = testcount/14
			print(script.Name)
			table.insert(sounds, clone)
			clone.Parent = part
			clone:Play()
		end
	end)
end
bugtest()

See my comment tacked onto the ā€œcoroutine.resumeā€ line. Basically when that line of code is removed, the sounds play every time. But with that line, the sound doesnā€™t audibly ā€œplayā€ the first 2 or so times. Even if the sound is preloaded or itā€™s Playing property is true, it doesnā€™t matter

Did you find out a solution? The same is happening to me.

Nope. I think a bug report should be posted about this. Iā€™m just surprised that there hasnā€™t been much talk surrounding this problem on the Forum, because Iā€™ve encountered this bug across multiple scenarios