How to make a .Touched event fire on multiple objects only once?

I’ve made a spin attack for my character, and when it collides with another Humanoid, it sends them away a few studs. The way this works is I’ve welded a cylinder part around my character to act as the ‘hitbox’ and it only becomes Touchable during the spin attack animation. I have a hitbox.Touched event that applies backwards velocity onto the Humanoid(s) that are hit by the attack.

--This is a server script that handles the physics. The .Touched event is in a local script.
kill.OnServerEvent:Connect(function(player,character,victim)
		local hitbox = character:WaitForChild("spinHitbox")
		local distanceX = victim.HumanoidRootPart.Position.X - hitbox.Position.X
		local distanceZ = victim.HumanoidRootPart.Position.Z - hitbox.Position.Z
		victim.HumanoidRootPart:ApplyImpulse(Vector3.new(distanceX*200,100,distanceZ*200))
end)

When testing this, I immediately noticed that even though the hitbox is only active during the spin attack animation, it still collides several times with other Humanoids, which multiplies their velocity exponentially. (See video below)

To combat this, I tried adding a debounce on the .Touched event, but this makes it so that only one Humanoid is affected at a time. (I want all enemies within range to be pushed back)

Btw, the models/animations are bad because it’s inspired by Supra Mayro 64.

1 Like

A debounce doesn’t have to be limited to a single object / variable!

You can instead create a table/array which is used instead. If the object is found in the table (table.find(TABLE, OBJECT)) then return. If not, add the object to the table (table.insert(TABLE, OBJECT)), then wait a few seconds, and then remove it again through
table.remove(TABLE, table.find(TABLE, OBJECT)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.