Hey! I’m making a combat system, using RaycastHitboxV4.
My thought process here was to create the hitbox object when the player first gains the tool, and to retrieve it when they attack with :GetHitbox(). However, my attacking here leads the script’s memory to increase and literally just not come back down.
I’ve gone through what I have thought is the pretty average process of making sure things can be garbagecollected by setting their variable to nil after destroying them, and doing the basic stuff like not putting events in events.
The actual memory usage isn’t really that bad, from my estimate its about 0.003 MB per attack. Here’s my memory usage specifc to the script, the black boxes are each time i attacked.
Here’s some time later, as we can see the memory usage literally just will not go down.
The following event is fired ONCE when the Tool containing the weapon is equipped.
Events.Melee.Register.OnServerEvent:Connect(function(Player, Tool)
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances = {Player.Character}
Attach(Tool.Weapon, Player.Character["Right Arm"])
local P1 = RaycastHitbox.new(Tool.Weapon)
P1.RaycastParams = Params
local P2
if Tool:FindFirstChild("Weapon2") then
Attach(Tool.Weapon2, Player.Character["Left Arm"])
P2 = RaycastHitbox.new(Tool.Weapon2)
P2.RaycastParams = Params
P2.OnHit:Connect(function(Hit, Hum)
MeleeHitReg(Hit, Hum, MeleeInformation[Player.Name.."AttackInfo"][1],MeleeInformation[Player.Name.."AttackInfo"][2],MeleeInformation[Player.Name.."AttackInfo"][3],MeleeInformation[Player.Name.."AttackInfo"][4],MeleeInformation[Player.Name.."AttackInfo"][5])
P1:HitStop()
P2:HitStop()
end)
end
P1.OnHit:Connect(function(Hit, Hum)
MeleeHitReg(Hit, Hum, MeleeInformation[Player.Name.."AttackInfo"][1],MeleeInformation[Player.Name.."AttackInfo"][2],MeleeInformation[Player.Name.."AttackInfo"][3],MeleeInformation[Player.Name.."AttackInfo"][4],MeleeInformation[Player.Name.."AttackInfo"][5])
P1:HitStop()
if P2 then
P2:HitStop()
end
end)
end)
This event is then fired after the player attacks, MeleeInformation is just a dictionary so we can retrieve information in the .OnHit function (shown in the Register event).
Events.Melee.Attack.OnServerEvent:Connect(function(Player, Tool, Length, Damage, SwingSound, HitSound, BlockBreaker, HitBlock)
local Character = Player.Character
PlayAudio(SwingSound, Character.Head)
RaycastHitbox:GetHitbox(Tool.Weapon):HitStart(Length)
if Tool:FindFirstChild("Weapon2") then
RaycastHitbox:GetHitbox(Tool.Weapon2):HitStart(Length)
end
MeleeInformation[Player.Name.."AttackInfo"] = {Damage, Player, BlockBreaker, HitSound, HitBlock}
end)
In case it’s needed, here’s my PlayAudio() function:
local function PlayAudio(Desired, Emitter)
local ToPlay = Desired:Clone()
ToPlay.Parent = Emitter
ToPlay:Play()
game.Debris:AddItem(ToPlay, ToPlay.TimeLength)
ToPlay = nil
end
Here’s the MeleeHitReg function as well:
local function MeleeHitReg(Hit, Hum, Damage, Player, BlockBreaker, HitSound, HitBlock)
local Character = Hit.Parent
local AttackedPlayer = game.Players:GetPlayerFromCharacter(Character)
if Character:FindFirstChild("Blocking") then
if BlockBreaker then
Hum:TakeDamage(Damage*1.5)
PlayAudio(RS.Effects.Sounds["Block Break"], Hit)
if AttackedPlayer then
Events.Melee.BlockBroken:FireClient(AttackedPlayer)
end
Events.Melee.VerifiedHit:FireClient(Player)
Events["Client Renderer"].RenderStunnedParticles:FireAllClients(Character)
Character.Blocking:Destroy()
else
PlayAudio(HitBlock, Player.Character.Head)
Hum:TakeDamage(Damage/4)
end
else
PlayAudio(HitSound, Player.Character.Head)
Hum:TakeDamage(Damage)
Events.Melee.VerifiedHit:FireClient(Player)
end
end