Stun system issues

Im trying to make a stun system for my combat game. So far the stun system kind of works. The opponent getting attacked kinda cant attack back in a combo. The opponent after 0.75 seconds can attack back even in a combo.

Im trying to figure out how to make the opponent get stunned longer when hit by another m1.

1 Like

You could stack more time on the stunned variable.

statsfol.Stunned3.Value = true
statsfol.Stunned2.Value += 1

repeat statsfol.Stunned2.Value = statsfol.Stunned2.Value - 0.01 wait(0.01)
until statsfol.Stunned2.Value <= 0

No idea what an m1 is but I’m assuming you just need to optimise your stun times, attack times, and other time dependent actions etc.

I recommend trying this:

if statsfol.Stunned2.Value < 0.15 then
statsfol.Stunned2.Value = 0.15
end

I can’t really explain very well why this would be more efficient it might not even fix your solution, but it’s worth a try. All I can say is it’s probably not the best idea to only wait until the values 0 when you reset it back to 0.15 per hit.

One of the problems I can see is:

wait(0.01)

Roblox runs at 60 fps, so the least amount of time you can wait for is ~0.017.
If you want to wait for one frame, use the Heartbeat or RenderStepped events.
Also, use task.wait(n), its the upgraded version of wait(n)

For the stun system, I would recommend using a counter, e.g.

local stunCounter = 0

function Stun(char, stunTime)
      stunCounter += 1 -- increments every time a stun occurs
      local stunNumber = stunCounter

      char.Stunned = true
      
      task.wait(stunTime)

      if stunNumber == stunCounter then -- if its not equal, another stun is occuring
            char.Stunned = false
      end
end

This method won’t stack stun, but makes stun follow up on each other, which imo is better.

If this is a server-side stun, then you will need to give each character their unique stun counter, like:

local stunCounters = {
    ["CharacterName"] = 0;
    ["CharacterName2"] = 0;
}

I tried it, but after two m1’s, there is a delay on the third. my task.wait() time was 0.65

I think your issue is that the variable resets only when it hits 0. You’ll want to try something like I did above, because if you don’t then if you punch a player twice the second punch won’t reset the timer because it will not be 0.

I hope this helps, I’m almost certain this is the problem as well especially if your combos are kind of slow.