How to make a counter attack for a pvp game

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a pvp game. And I want the ability I’m working to have a counter move which will attack the opponent back if he is trying to hit me.

  2. What is the issue? Include screenshots / videos if possible!
    So I guess if I want to counter a move, I need to add a value to a player which will be a counter value. But I don’t want to detect it from every scripts that does an attack. I want to know if there’s any easier way to do this?

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried searching about making counter attacks but I can’t seem to find anything related to it

How my game damage system works is we do the animation of attack and sound on the client then do FireServer to let the server detect if the hitbox hits any entity then deal damage.

I hope explaining how the damage system of my game works would help

Thanks

3 Likes

You could potentially use a bindable function or something.
What a bindable function is a client to client or server to server connection that you can return a value.

How it would work is like this.

Player1 attacks player 2, there is a quick check to see if player 2 has a counter
If player 2 has a counter here is the sequence of events.

  1. Player 1 invokes a bindable event to player 2
  2. Player 2 has .1 seconds to react to that bindable function
  3. they return the result.

Here is some example scripts just generally showing what could be done

-- player 1
local player = game:GetService('Players').LocalPlayer

function attack(player2)
   if table.find(getMoves(player2), 'counter') then -- Shows that player 2 has a counter
      local countered = player2.attackBind.Invoke(player)
      if countered == false then
         -- They didn't counter continue with attack sequence
      end
   end
end
-- player 2
local player = game:GetService('Players').LocalPlayer
local bindEvent = player:WaitForChild("AttackFunction")

local countering = false -- this variable you change when the player 2 is countering

bindEvent.OnInvoke = function(otherPlayer)
   wait(.1)
   return countering -- Return if they are countering or not
end

This is really the barebones though, you need to create your own custom checks if either player is lagging, and also you would want server sanity checks since players could abuse this with hacks or something. Hoped this helped to get you started though

6 Likes

I have tried looking into BindableEvent and study some of it. Your method really helps making this so much easier! Thank you!

2 Likes