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)