Not allowing a player to attack after being hit

Hello, I’m making a combat system and I’m attempting to disable a player’s ability to attack (via mouse1) when they are attacked. So far I’m using a task.spawn and waiting 1 second before the player is able to attack again. This method is unreliable as the player is eventually able to continue attacking during an m1 combo.

I’m currently thinking of another way, using a heartbeat function and a local script to iterate through an interval and when the player is attacked the interval is set back to 0, and when that interval is reached it fires back to the server allowing the player to attack again.

If anyone has any more reliable ways of going about this, I would appreciate if you would share them, thank you for your time.

(Screenshot of what a player experiences when their attacked)

You can use if to check if have a player/dummy in Hitbox and don’t put the CanDamageAnotherPlayer in true.

Like this:

task.spawn(function()
   If PlayerInHitbox == false then
       task.wait(1)
       humanoid.Parent.Attack.Value = false
    end
end)

The PlayerInHitbox is defined in a function that check if a player or dummy is on Hitbox.

Sounds like you are trying to implement stun/debounce, but are doing it extremely indirectly.

Here’s my take here, if you want stun that affects movement & attacking - use a Stunned IntValue/attribute (I prefer attributes, but given you use values you’d probably use them).

Whenever the target gets hit, you would increment the Stunned value: target.Stunned.Value += 1 and then, via task.delay you would decrement the value in N seconds, so it returns to 0: target.Stunned.Value -= 1

Considering you also seem to set the WalkSpeed, you can do it whenever adjusting the Stunned value. Just, when you revert it, add a simple check to see whether the target is still stunned or not.

if target.Stunned.Value == 0 then
    humanoid.WalkSpeed = 16
end

Whenever the player wants to attack, check if the Stunned value is above 0, in which case return & do nothing.

Would this prevent the player from sneaking in attacks while they’re getting comboed? For example, the issue right now is that after my task.wait(1) in the bottom of my code once the wait is over the player is able to attack while they’re already in a combo, which I’m trying to prevent. Correct me if I’m wrong but wouldn’t this still allow the player being attacked to attack after the N seconds even while they themselves are in a combo?

For clarity, I’m doing a 5 hit M1 combo system

Yes, that is exactly what my goal was here. If you use a BoolValue to determine stun, it’ll get overridden by already awaiting threads (so, if you get stunned while stunned, the first stun will cancel it out early). Here, whenever a stun ends, it’ll just decrement the value, meaning the stun remains because the value will still be >0

The player is still able to M1 in between m1 combos, did I do this right? I’m not sure.

When the player is hit, they need to change the “CanDamageAnotherPlayer” value, I don’t see this changing in your script except de task.spawn function

image


It should check for char.AttackInt.Value > 0. If the melee combat is partly handled on the client (animations & hitbox), also have it check there.

Apologies, the “CanDamageAnotherPlayer” variable is being changed in a local script and being fired from the “cancelM1:FireClient()”

The “char” it’s initially checking is the attacker’s “CanDamageAnotherPlayer” and not the player being attacked

Okay I tried adding the check, this is the code now:

This is what I’m checking for in the local script, specifically if the player presses Mouse1

image

It’s still pretty much acting the same way, I’m very confused. Would using remote functions help?

I’m thinking of a way that fires an OnClientEvent to the local script that basically increments an interval until it reaches some number and checks a value/attribute to stop the player from being able to attack. Then fires back to the server to allow the player to be able to attack. If the player is hit, then the interval is returned to 0 and the loop continues until it’s complete.

Do you think this would cause any problems?

If you did it as I expect it to be, it should work just fine. I am not too certain on how you did it, but let me try to explain where I would put the checks:

  1. Before running melee, so it doesn’t happen in the first place
  2. When the server receives a hit / melee request, check right in the start - reject if stunned.

In theory, this should stop it from interrupting combos???

It’s working now, thank you and sorry for the trouble lol. I must’ve been trying it in an old team test with the code not being updated. I will definitely be thinking about using IntValues like this more often.