local UserInputService = game:GetService("UserInputService")
local HttpService = game:GetService("HttpService")
local MouseButton1 = false
local Sound = nil
UserInputService.InputBegan:Connect(function(InputBegan)
if InputBegan.UserInputType == Enum.UserInputType.MouseButton1 then
MouseButton1 = true
Sound = Instance.new("Sound")
Sound.Name = HttpService:GenerateGUID()
Sound.SoundId = "http://www.roblox.com/asset/?id=2760979"
Sound.Parent = game:GetService("Players").LocalPlayer.Character
while wait(0.01) and MouseButton1 do
Sound:Play()
end
end
end)
UserInputService.InputEnded:Connect(function(InputEnded)
if InputEnded.UserInputType == Enum.UserInputType.MouseButton1 then
MouseButton1 = false
if not Sound.IsPlaying then
Sound:Destroy()
end
end
end)
More info required.
Is the sound playing but not stopping?
Any errors in the output window?
Is the sound looped?
I’m looking at “if not Sound.IsPlaying then” and thinking that this function is only being called once, and if the sound is still playing then it skips past that if loop and ends the function so the sound isn’t being Destroyed.
Why is the sound being destroyed every time? Why not just Stop it?
the Sound instance is not destroyed because when you do this not Sound.IsPlaying, the sound continues to play. By making this change the sound instance is destroyed.
UserInputService.InputEnded:Connect(function(InputEnded)
if InputEnded.UserInputType == Enum.UserInputType.MouseButton1 then
print("UnSound")
MouseButton1 = false
Sound.Ended:Wait()
Sound:Destroy()
end
end)
But there is another problem. if you make several clicks very fast some Sound instances will not be destroyed, because the Sound variable is global and when you make clicks very fast you lose the reference of the previous Sound instance and only the current one will be destroyed. a possible solution is to make the Sound variable local and manipulate it in a coroutine.
local UserInputService = game:GetService("UserInputService")
local HttpService = game:GetService("HttpService")
local MouseButton1 = false
--local Sound
UserInputService.InputBegan:Connect(function(InputBegan)
if InputBegan.UserInputType == Enum.UserInputType.MouseButton1 then
MouseButton1 = true
local Sound = Instance.new("Sound")
Sound.Name = HttpService:GenerateGUID()
Sound.SoundId = "http://www.roblox.com/asset/?id=2760979"
Sound.Parent = game:GetService("Players").LocalPlayer.Character
coroutine.wrap(function ()
while wait(0.01) and MouseButton1 do
Sound:Play()
end
if not Sound.IsPlaying then
Sound:Destroy()
end
Sound.Ended:Wait()
Sound:Destroy()
end)()
end
end)
UserInputService.InputEnded:Connect(function(InputEnded)
if InputEnded.UserInputType == Enum.UserInputType.MouseButton1 then
MouseButton1 = false
end
end)
The issue is probably here - if you want the sound to be destroyed when it is playing, you need to remove the logical not operator - otherwise it will only go into the if statement block when it is not playing.
Also suggest checking if Sound is nil before trying to index or otherwise do any operation on it later after instantiation - otherwise you could get errors involving it being nil filling your output over time.
If you want Sound to be destroyed regardless, I’d suggest just checking if Sound is not nil instead to make sure your code doesn’t error.