How to correctly Time an SFX with an attack move?

Hi there, right now I have a script that once a key is pressed, it will move the character over to the player of whom the mouse is hovered over and slice them with an animation while dealing damage.

The problem is trying to figure out the SFX timing, if I put “StartSfx:Play()” before the active attack code, it will play the sound effect before and if I place the play() code underneath the attack code, it plays after.

How do I make the sound effect play MID attack rather than at the start or at the end.

Code:

elseif input.KeyCode == Enum.KeyCode.Y and SwordOut.Value and CanAttack.Value and not Attack_5 then
   spawn(function()
	 local ActiveSFX = true
	 local properties = {FieldOfView = 70}
	 Attack_5 = false
   if ActiveSFX == true then 
	Attack_5 = true
	Attack_5 = Remotes:WaitForChild("WT_5Style_RF"):InvokeServer(tool,AnimationsFolder,Mouse.Hit)
	local StartSfx = game.ReplicatedStorage.Sounds2.SwordHit3:Clone() -- Sound Effect
	StartSfx.Parent = Player.Character.HumanoidRootPart
	StartSfx.MaxDistance = 150
	StartSfx.TimePosition = 0
	StartSfx:Play()
   end
end)

What the attack looks like:

P.S I’m new to lua so please if I state the obvious, correct me.

You should look into threading, but another thing you need to be able to do is understand how long it will take for certain block of code to run. The simple solution to this is to use task.delay and wait a certain amount before playing the sound.

task.delay(theAmountOfTime, function()
    sound:Play()
end)
1 Like

Thanks that worked, but it still plays when the move is on cooldown or if the attack isn’t currently active, how do I limit it to just the attack* while it’s active?

That’s probably a flaw on the way your attack framework is designed, because this shouldn’t be a problem. You have to place it in the same place that you place the main code, I recommend right before the code runs, but not outside of the block, as that will cause unwanted behavior.

1 Like

Ok cheers, I’ll keep trying to tinker.

1 Like

Hi Den, thanks for the suggestion, I found the solution that times it properly with a task delay like you said and with inputting the player’s mouse hover.

The problem is, if the attack is out of range, and the button is still hovering over the player, the sound will still play.

How do I make an IF statement that checks whether the player is within range first before playing sound effect?

Well in range of what? Give me more details.

1 Like

I’m being stupid sorry, realised I was trying to manipulate the data from the animation script file rather than the actual script. You were correct about the actual framework being wrong lol, my bad.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.