Hello, I’m going to try to keep this short. I need help thinking of a way to check if an enemy player (or npc) is being attacked.
Originally, I thought of using a boolean attribute to check if a player is getting attacked, the problem with this however is I am using task.delay() to remove the attribute after a certain amount of time. This creates a problem no matter if the enemy is getting hit by 2 or more players, it would become unstable and start toggling the boolean on and off.
it would be better to just use a numbervalue that counts down in milliseconds, enemy gets hit = set the numbervalue to the time the player will be marked as attacked for = count down in a loop, if the player gets hit again while the loop is counting down then just set the value back to the max value
This can again lead back to a problem of multiple attackers. The last attack will have the priority of the NumberValues ms.
Imagine I have a move that temporarily staggers a player for 2.5 seconds, then another move that staggers for 5 seconds, this value would only apply to the last move hit by the player. I could technically add both together but that can then lead to a problem of a stun that lasts too long. I feel this method can just permiate more problem than solutions. I’m really in a drought right now and I can’t think of a good way of handling this.
the only way to fix this that i have on my mind right now is creating a folder that holds all the values of stuns and each of them counting down separately but thats probably bad performance wise
OR use the method you used in the post and use task.cancel on the task.delay when a new enemy attacks
if newcontroltime > controltime then
controltime = newcontroltime
end
if Controlcoroutine then
coroutine.close(Comtrolcoroutine)
end
coroutine.create(function()
game.[“Run Service”].Stepped:Connect(function(t,dt)
controltime -= dt
if controltime <= 0 then
If you want this to work like stun from a fighting game, where the newest stun takes priority, then you’ll want to use an identifier. Basically, we have a value, and each time the function is called, that value is updated to be different. Once the function is finished waiting, it checks to see if the identifier value has changed. Should it be the same, that means the function hasn’t been called again, otherwise it has. Here’s an example:
local G_Increment = 0
local Attacked = false
function SetAttacked()
local L_Increment = G_Increment + 1 --// local scope.
G_Increment += 1 --//Update identifier.
Attacked = true
task.wait(1)
--//If L_Increment ~= G_Increment, that means something else has changed it.
if L_Increment == G_Increment then
Attacked = false
else
print("Interrupted!")
end
end
I forgot to reply to this forum, I ended up replacing the boolean with a number value and incrementing it based n the amount of attacks the enemy is attacked with.