How can I stop playing a Local Sound?

I want to make a Loading Screen that plays an intro music when you play the game, but I want the intro music to stop when the user selects which team he’s on. I used SoundService:PlayLocalSound for this, but I can’t seem to stop the music when i call :Stop on the sound. Code (part of it) in LocalScript in ReplicatedFirst

-- Show loading screen and wait for game to be loaded (game.Loaded:Wait())
-- Play Intro music
local SoundService = game:GetService("SoundService")
local sound = game.ReplicatedStorage.Sounds:WaitForChild("Intro"):Clone()
SoundService:PlayLocalSound(sound)
game.Players.LocalPlayer.PlayerGui.ScreenGui.SwitchTeam.Red.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.SwitchTeam:FireServer("Red")
    sound:Stop()
    sound:Destroy()
end)
1 Like

You can use the .Ended() function, like this:

sound.Ended()

but according to the api reference, it says its an event to connect to, not a function

1 Like

Do you get any warnings or errors from that script that may be stopping it?

only got this error: image

for i,v in pairs(workspace.Camera:GetChildren()) do
	if v.Name == "Loading Sound" then
		v:Stop()
		v:Destroy()
	end
end


local sound = Instance.new("Sound", workspace.Camera)
sound.Volume = 1
sound.SoundId = "rbxassetid://5195523442"
sound.Name = "Loading Sound"
sound.Looped = true
sound:Play()


if sound.Looped ~= true then
	game:GetService("Debris"):AddItem(sound, sound.TimeLength)
end


game.Players.LocalPlayer.PlayerGui.ScreenGui.SwitchTeam.Red.MouseButton1Click:Connect(function()
  game.ReplicatedStorage.SwitchTeam:FireServer("Red")
	if sound ~= nil then
		sound:Stop()
		sound:Destroy()
	end
end)
2 Likes

You’re problem is that sounds don’t work properly in ReplicatedStorage. Move the sound into workspace or another environment where they can be heard.

1 Like