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:

5 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