Attacks with sounds

I have an attack that I made in Roblox studio that activates when you press E and hold E, when you let go the attack will stop, its called a stand barrage if you are familiar with that, I want to give it a sound audio which I already have (the audio),
but I am not sure how to make it so it will play the sound while it does the barrage punches and at any time if you let go the sound will stop the sound is 9 seconds, the maximum barrage time is 10 seconds and it will stop after so I think it will be perfect, any help is appreciated.

It’s fairly easy, just link it to your attack code like so:

-- Holding E
AttackSound:Play()
-- ...

-- Let go of E
AttackSound:Stop()
-- ...

You may also want to loop the audio in the Properties window if the sound loops well and want to increase the length of the attack.
Don’t worry about resetting the audio either, using :Stop() automatically sets the “TimePosition” to 0. If you don’t want that to happen, use :Pause().

1 Like

Then should my script look like this or did I do it wrong

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

local TweenService = game:GetService(“TweenService”)

local Animations = script:WaitForChild(“Animations”)

local AttackSound = script:WaitForChild(“AttackSound”)

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,-2))
					local info = TweenInfo.new(.25)
					local Tween = TweenService:Create(Controller,info,goal)
					Tween:Play()
					
					AttackSound:Play()
					
					Tween.Completed:Connect(function()
						Tween:Destroy()
						
						local Punching = AnimControl:LoadAnimation(Animations.Barrage)
						Punching: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()
										AttackSound: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)

I don’t know, try it. If it doesn’t work, check the console/output for errors. I don’t know how your game is designed, so I can’t help you with everything. Your attack sound should be in a part, so that it has an origin to play from, otherwise you’d be able to hear it everywhere.

I tried it and it didn’t work for me there were no errors in the console, I want it to be heard from everywhere because I would like other players with stands to be able to hear the barrage of course

When I said “everywhere” I meant even if you were, lets say, 100000 studs away. It probably also doesn’t work anyway because it’s parented to a script, and not the workspace (if you wish to hear it “everywhere”)