How to end audio after playing for x amount of seconds

how do i play an audio when player is loaded into a game, and then stop it after it has played for 5 seconds. I want it to fade out. I’ve tried alot of scripts and no luck.

You can just simply go ahead and do a if statement, probably best with a loop to keep checking.

local Sound = WhereYourSoundIs
if Sound.TimePosition == 5 then
 Sound:Stop()
end

@UhhQuoi is right, but I would go like this:

local Sound = WhereYourSoundIs
if Sound.TimePosition >= 5 then
 Sound:Stop()
end
1 Like

This should work:

local Sound = WhereYourSoundIs
if Sound.TimePosition >= 5 then
	for i = 0.1, Sound.Volume do
		Sound.Volume = Sound.Volume -0.1
		wait(0.1)
	end
	Sound:Stop()
end

The sound is in workspace. I put the script in workspace as well under a local script. I only want the client to hear it.

I took your script and edited it:
local Sound = workspace.game.S1
if Sound.TimePosition >= 5 then
for i = 0.1, Sound.Volume do
Sound.Volume = Sound.Volume -0.1
wait(0.1)
end
Sound:Stop()
end

It does nothing.

put the local script to the starterplayerscripts

you need to start the song, and keep checking many times, this should work:

local Sound = workspace.game.S1

Sound:Play()

while wait() do
	if Sound.TimePosition >= 5 then
		for i = 0.1, Sound.Volume do
			Sound.Volume = Sound.Volume -0.1
			wait(0.1)
		end
		Sound:Stop()
	end
end

Still nothing. I put it into starterplayerscripts.

Do you mean enable the option for “playing” in the audio? Where do I put the script? I tried your script still nothing.

make a remote event called “Loaded” on the replicatedstorage
and make a script on the server script,
then use this code:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		game.ReplicatedStorage.Loaded:FireClient(player) -- if loaded then fire
	end)
end)

and you need to put local script in the starterplayerscripts
and put this code in:

game.Workspace.game.S1:Play()
local Sound = game.Workspace.game.S1

game.ReplicatedStorage.Loaded.OnClientEvent:Connect(function() --listen the event
	wait(5) -- wait for 5 seconds
	for i = 1, 18 do -- fade the sound
		Sound.Volume -= 0.1
		wait(0.1)
	end
	Sound:Stop() -- stop the sound
end)

hope this works

No still nothing. I did all the steps. I put the Remote event in replicatedstorage and renamed it to Loaded. Then i put a script into serverscriptservice and used your script. Afterwards I put a local script into starterplayerscripts and used your script and tested it out a few times. Still nothing. Just silence.

Try something along the lines of.

Wait(5)
Game.Workspace.AUDIONAME.Volume = 1 
wait(0.1)
Game.Workspace.AUDIONAME.Volume = 0.8
wait(0.1)
Game.Workspace.AUDIONAME.Volume = 0.6
wait(0.1)
Game.Workspace.AUDIONAME.Volume = 0.4
wait(0.1)
Game.Workspace.AUDIONAME.Volume = 0.2
wait(0.1)
Game.Workspace.AUDIONAME:Stop()

Hm, But when i test it, it works fine, maybe try to check your sound volume and make sure its not 0

I think I know what the issue is. There is something I’ve never seen before called “commiting” when i tried to publish the game the scripts that i made from you were not “committed”. What does that mean?

There is no code to play the audio? Do I enable the playing option for the audio? Where do i put this script?

Commit means to publish a script so other people can see it, if a script is not committed only you can see the script. For others. Nothing changes.

Since GetPropertyChangedSignal does not work on TimePosition (not sure why), we can simply constantly check if the TimePosition >= 5

local Sound = game:GetService("SoundService"):WaitForChild("SOUNDNAME") -- the sound instance should be parented inside of SoundService
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out) -- .5 being the time to fade out
Sound:Play()

while Sound.IsPlaying and task.wait() do
    if Sound.TimePosition >= 5 then
          local tween = TweenService:Create(Sound, tweenInfo, {Volume = 0})
          local con
          tween:Play()

          con = tween.Completed:Connect(function()
              con:Disconnect()
              Sound:Stop()
          end)
          break
    end
end

Should work, might be some syntax issues since I wrote it here.

1 Like

try to commit it (30303030303030303030)

Thanks it worked. Took a while but thanks.