long story short i created an overcomplicated hitbox handler system that was supposed to help with multiple hitboxes (.i.e, spawns 7 hitboxes, but if one of those hits a target, the other 6 can’t) and it works, but the touched event is firing weirdly. Another thing is that using 1 hitbox seems to fire more often than 7, so I do not know why it is doing that.
The original script used collectionService, but it basically did the same thing as this.
The event is VERY inconsistent, sometimes firing when the first hitbox spawns touches something, other times when all the other hitboxes have despawned and the last hitbox is there, etc.
is it just the .touched event being weird and is there a better way to do this?
--one function
function OnHitboxHit(OtherPart, Hitbox, Pack: Packet)
--Get the parent model of the one who was hit
local ParentModel = OtherPart.Parent
if ParentModel == Hitbox.Parent then
--Do not continue with this function.
return
end
if Pack.ModelsHit[ParentModel] == true then
--Do not continue with this function.
return
end
--If the one who was hit is a teammate then...
if ParentModel:FindFirstChild("Teammate") then
--Do not continue with this function.
return
end
--If the one who was hit was the one who sent it then...
if ParentModel.Name == Pack.Sender.Name then
--Do not continue with this function.
return
end
--If all the above do not apply, find the one who was hit's humanoid
local Humanoid: Humanoid = ParentModel:FindFirstChild("Humanoid")
--If the Humanoid was found then...
if Humanoid then
--print("Enemy hit!")
--If the hitbox is able to apply an effect then...
if Hitbox:FindFirstChild("Effect") then
--Apply an effect.
StatusModule.AddTag(OtherPart.Parent, {Hitbox.Effect.Value}, {Hitbox.Effect.Intensity.Value}, {Hitbox.Effect.Duration.Value}, {Hitbox.Effect.Type.Value}, {Hitbox.Effect.TagName.Value})
end
--Gets the hitbox's damage value
local damage = Hitbox.Damage.Value
--If the enemy is unable to have stats then...
if not ParentModel:FindFirstChild("Stats") then
--Deal damage unaffected by the attacked's stats
Humanoid:TakeDamage(damage)
else
--Else deal damage that is affected by the attacked's stats
Humanoid:TakeDamage(damage / ParentModel.Stats.DefenseMultiplier.Value)
end
--Log this model as hit
Pack.ModelsHit[ParentModel] = true
--If the hitbox is able to hit multiple times then...
if Hitbox:FindFirstChild("HitCooldownTime") then
--Wait the designated amount
task.wait(Hitbox.HitCooldownTime.Value)
Pack.ModelsHit[ParentModel] = false
end
end
end
--another function
function DamageHandler.CreateNewGroup(PhysicalFolder: Folder, sender: Model): Packet
local NewPacket = {
PhysFolder = PhysicalFolder,
Sender = sender,
Connections = {
OnAddConnection = nil,
OnRemoveConnection = nil,
OnDestroyedConnection = nil,
TouchedConnections = {}
},
ModelsHit = {},
Hitboxes = {}
}
NewPacket.Connections.OnAddConnection = NewPacket.PhysFolder.ChildAdded:Connect(function(Child: BasePart)
local Conn = Child.Touched:Connect(function(OtherPart)
OnHitboxHit(OtherPart, Child, NewPacket)
end)
NewPacket.Connections.TouchedConnections[Child] = Conn
print("applied connection!")
end)
NewPacket.Connections.OnRemoveConnection = NewPacket.PhysFolder.ChildRemoved:Connect(function(Child)
NewPacket.Connections.TouchedConnections[Child]:Disconnect()
--table.remove(NewPacket.Connections.TouchedConnections, NewPacket.Connections.TouchedConnections[Child])
NewPacket.Connections.TouchedConnections[Child] = nil
print("removed connection!")
end)
NewPacket.Connections.OnDestroyedConnection = NewPacket.PhysFolder.Destroying:Connect(function()
NewPacket.Connections.OnAddConnection:Disconnect()
NewPacket.Connections.OnRemoveConnection:Disconnect()
end)
table.insert(DamageHandler.Packets, NewPacket)
--print("created new packet!")
return NewPacket
end
p.s. the hitbox is constraint welded to a part i placed at the world root (0,0,0), and it is unanchored. it is also stored in a folder within the player.