Sound wont stop when letting go of E

I have made an attack in Roblox studio its a barrage and now I have figured out how to make it play a sound when it starts the attack but the problem is when I let go of the letter E (which starts the attack, you have to hold it) it doesn’t stop the sound and it keeps playing until the sound ends how can I make it so that the audio can stop at any time when letting go of the letter E?
this is the script where I made it play the sound

local rp = game:GetService(“ReplicatedStorage”)
local Barrage = rp:WaitForChild(“Barrage”)

local TweenService = game:GetService(“TweenService”)

local Animations = script:WaitForChild(“Animations”)

local SSS = game:GetService(“ServerScriptService”)
local Library = SSS:WaitForChild(“Library”)
local DictionaryHandler = require(Library:WaitForChild(“DictionaryHandler”))

local Barrage_Handler = require(script.Barrage_Handler)

local maxDuration = 10

Barrage.OnServerEvent:Connect(function(Player,isActive)
local Character = Player.Character
local Humanoid = Character.Humanoid
local HumanoidRP = Character.HumanoidRootPart

if not DictionaryHandler.findPlayer(Humanoid,"Stunned") and not DictionaryHandler.findPlayer(Humanoid,"Blocking") then
	if isActive then
		local Stand = Character:FindFirstChild("Stand")
		if Stand then
			
			local AnimControl = Stand:FindFirstChild("AnimControl")
			if AnimControl then
				local Controller = Stand.PrimaryPart:FindFirstChild("Controller")
				if Controller then
					local Folder = Instance.new("Folder",Character)
					Folder.Name = "Effects"
					
					Humanoid.WalkSpeed = 8
					Humanoid.JumpPower = 0
					
					local goal = {}
					goal.C0 = Controller.Part0.CFrame:ToObjectSpace(Controller.Part1.CFrame)
					goal.C1 = Controller.Part0.CFrame:ToObjectSpace(Controller.Part1.CFrame * CFrame.new(0,.5,-3))
					local info = TweenInfo.new(.25)
					local Tween = TweenService:Create(Controller,info,goal)
					Tween:Play()
					
					
					Tween.Completed:Connect(function()
						Tween:Destroy()
						
						local Punching = AnimControl:LoadAnimation(Animations.Barrage)
						Punching:Play()
						local Sound = script.AttackSound:Clone()
						Sound.Parent = Character.PrimaryPart
						Sound:Play()
						
						Barrage_Handler.punches(Character,Stand,Folder)
								
						
						
						spawn(function()
							wait(maxDuration)
							local Folder = Character:FindFirstChild("Effects")
							if Folder then
								Humanoid.WalkSpeed = 16 
								Humanoid.JumpPower = 50

								local goal = {}
								goal.C0 = Controller.Part0.CFrame:ToObjectSpace(Controller.Part1.CFrame)
								goal.C1 = Controller.Part0.CFrame:ToObjectSpace(Controller.Part1.CFrame * CFrame.new(-3,1,2))
								local info = TweenInfo.new(.25)
								local Tween = TweenService:Create(Controller,info,goal)
								Tween:Play()
								
				
								for _, track in pairs(AnimControl:GetPlayingAnimationTracks()) do
									if track.Name == "Barrage" then
										track:Stop()
											
									     Sound:Stop()
									end
								end
								Barrage_Handler.cleanUp(Folder)
							end
						end)
					end)
				end
			end
		end
	else
		local Stand = Character:FindFirstChild("Stand")
		if Stand then

			local AnimControl = Stand:FindFirstChild("AnimControl")
			if AnimControl then
				local Controller = Stand.PrimaryPart:FindFirstChild("Controller")
				if Controller then
					
					local Folder = Character:FindFirstChild("Effects")
					if Folder then
						Humanoid.WalkSpeed = 16 
						Humanoid.JumpPower = 50

						local goal = {}
						goal.C0 = Controller.Part0.CFrame:ToObjectSpace(Controller.Part1.CFrame)
						goal.C1 = Controller.Part0.CFrame:ToObjectSpace(Controller.Part1.CFrame * CFrame.new(-3,1,2))
						local info = TweenInfo.new(.25)
						local Tween = TweenService:Create(Controller,info,goal)
						Tween:Play()

						for _, track in pairs(AnimControl:GetPlayingAnimationTracks()) do
							if track.Name == "Barrage" then
						   track:Stop()
							end
						end
						Barrage_Handler.cleanUp(Folder)
					end
				end
			end		
		end
		Barrage:FireClient(Player)
	end
	
else
	Barrage:FireClient(Player)
end

end)

Assuming that your event “Barrage” is fired from the client based on a User Input, you can use [Input Ended] (UserInputService | Roblox Creator Documentation) event of UserInputService to detect when E is let go of.

You could use this to fire an event to the server to get it to stop the barrage animation and sound.

2 Likes

Yes it fired based on a user input I think but I don’t know where to begin with it I have Input ended In my script look at this could you tell me where I should begin with it
local Player = game:GetService(“Players”).LocalPlayer

local Group = script.Parent

local SSS = game:GetService(“ServerScriptService”)

local rp = game:GetService(“ReplicatedStorage”)

local Barrage = rp:WaitForChild(“Barrage”)

local UIS = game:GetService(“UserInputService”)

local runservice = game:GetService(“RunService”)

local Debounce = false
local isActive = false
local cd = 3

UIS.InputBegan:Connect(function(input,isTyping)
if isTyping then
return
elseif input.KeyCode == Enum.KeyCode.E then
if Debounce == false and isActive == false and Group:GetAttribute(“active”) == false then
Debounce = true
isActive = true

		Group:SetAttribute("active", true)
		
		Barrage:FireServer(isActive)
		wait(.1)
		Debounce = false 
	UIS.InputEnded:Connect(function(input)	
	if input.KeyCode == Enum.KeyCode.E then
		if Debounce == false and isActive == true  and Group:GetAttribute("active") == true then
			Debounce = true
			isActive = false
			
			Group:SetAttribute("active", false)
			
				Barrage:FireServer(isActive)	
					end
	              	end
                	end
              end)
	       end
    end

Barrage.OnClientEvent:Connect(function()
Group:SetAttribute(“active”, false)

wait(cd)
Debounce = false
isActive = false
end)

end)

You can use the same function you created before

Barrage.OnClientEvent:Connect(function()
for _, track in pairs(AnimControl:GetPlayingAnimationTracks()) do
	if track.Name == "Barrage" then
		track:Stop()						   
	end
end
 Sound:Stop()

Group:SetAttribute(“active”, false)
wait(cd)
Debounce = false
isActive = false
end)
end)
2 Likes

Yes, but I would need to get AnimControl so I would also need to get the Stand but the Stand is in ServerScriptService and whenever I try to call Stand, of course, it just wouldn’t work because this is a local script in StarterPack, any solutions for this?

The script I sent above is for server script service.

On your local script, you fire to the server whenever E is pressed, and whenever E is let go.

On your server script you listen for both events.
For event InputBegan, you connect the function initiating the animation and sound.

For event InputEnded, you connect the function ending the animation and the sound.

1 Like