How would I make an attack holdable?

Hello, I’m making a game where there’s an attack you can charge, but I want it so that if you let go of the key then it’ll do the attack, how would I add this to my current code?

FYI: If you haven’t noticed by reading the script, a local script fires the key event whenever a key is pressed)

the server script:

(The server script charges it by itself, how would I make it so that also if you let go of Z before the wait(3) ends it does that attack but smaller?)

game.ReplicatedStorage.KeyEvent.OnServerEvent:Connect(function(plr, key)
	local character = script.Parent.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if key == "Z" then
		local ChargeAttack = game.ServerStorage.ChargedAttacks.Charge1:Clone()
		ChargeAttack:PivotTo(character.PlrLockedOnto.Value.HumanoidRootPart.CFrame)
		ChargeAttack.Parent = workspace
		wait(3)
		local ChargeAttackBeam = game.ServerStorage.ChargedAttacks.Beam:Clone()
		ChargeAttackBeam:PivotTo(ChargeAttackBeam.Root.CFrame)
	end
end)

(Also, I already have a key ended event, I’m just wondering how I would implement it, it’s in replicated storage with the key event.)

Not sure what the client looks like, but you could possible have a button down function for your key, start the charging state, and record a Tick() when you initially press the key down.
You can then create an event when the key is lifted, get the current Tick(), subtract the new one by the previously recorded one, and carry out the attack state, using the Tick() as a scale for smaller attacks.

More info on what a Tick() is here:

You could fire the remote to server whenever the player presses and release the key. And whenever the player releases the key it checks if time has passed 3 seconds using tick() as @MSYGamingTV said, if not do a small attack

1 Like

Could you give me an example of code? I’m a bit confused.

(Also, I already have a key ended event and it gets fired when you stop pressing a key, I’m just wondering how I would implement it, it’s in replicated storage with the key event.)