I have been using this hitbox script for a while for personal projects. However, I believe there are more optimal ways to make a hitbox system. My goal of this code review is just learning a new way on how to make hitbox system because I’ve heard that .Touched is outdated and an optimal way of making modules.
local module = {}
module.Timer = {}
module.HitBox = function(size, damage, shape, cframe, plr, count, plrknockback, enemyknockback, up, hitvfx)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:FindFirstChild("Humanoid")
local hrp = char:FindFirstChild("HumanoidRootPart")
local ht = {}
local hitBox = Instance.new("Part", workspace)
local tz = os.time()
hitBox.Size = size * Vector3.one
hitBox.Shape = shape
hitBox.CFrame = hrp.CFrame * cframe
hitBox.Anchored = true
hitBox.CanCollide = false
hitBox.Transparency = 1
print(count)
hitBox.Touched:Connect(function(hit)
if hit.Name == "HumanoidRootPart" and hit.Parent.Name ~= plr.Name and hit.Parent:FindFirstChild("Humanoid") then
if table.find(ht, hit.Parent.Name) then return end
local creator = Instance.new("ObjectValue")
creator.Value = plr
creator.Name = "creator"
game.Debris:AddItem(creator, 0.6)
hit.Parent:FindFirstChild("Humanoid").Died:Connect(function()
creator.Parent = hit.Parent:WaitForChild("Humanoid")
print(creator)
if creator and creator.Value then
plr.leaderstats.Kills.Value += 1
end
end)
task.spawn(function()
if hitvfx then
hitvfx.CFrame = hit.Parent:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0,math.random(0,1.5),-1)
for i, v in pairs(hitvfx.Attachment:GetChildren()) do
task.wait()
v:Emit(v:GetAttribute("EmitCount"))
end
else
hitvfx = nil
end
end)
local Direction = (char:FindFirstChild("HumanoidRootPart").Position - hit.Parent:FindFirstChild("HumanoidRootPart").Position).Unit
local dot = hit.Parent.HumanoidRootPart.CFrame.LookVector:Dot(Direction)
if count == 4 and hit.Parent:GetAttribute("Blocking") == true then
hit.Parent:SetAttribute("GuardBreak", true)
print('guardbreak')
damage = damage/2
elseif count == 4 and hit.Parent:GetAttribute("Blocking") == false then
task.spawn(function()
enemyknockback = 200 -- make sure to lower the enemyknockback so that the player doesn't go flying with the enemy or just lower players knockback
print(enemyknockback) -- oh and make sure to add like an animfreeze tech
hit.Parent.HumanoidRootPart.AssemblyLinearVelocity = hrp.CFrame.LookVector * enemyknockback
task.wait(1.5)
enemyknockback = 35
print(enemyknockback)
end)
elseif count == 5 and hit.Parent:GetAttribute("Blocking") then
damage = math.floor(damage/4)
hit.Parent:SetAttribute("GuardBreak", true)
print('guardBrekofdeath')
end
if not up then
hit.Parent.HumanoidRootPart.AssemblyLinearVelocity = hrp.CFrame.LookVector * enemyknockback
hrp.AssemblyLinearVelocity = hrp.CFrame.LookVector * plrknockback
else
hit.Parent.HumanoidRootPart.AssemblyLinearVelocity = hrp.CFrame.UpVector * enemyknockback
end
if dot > 0 and hit.Parent:GetAttribute("Blocking") == true then
print('yes')
damage = math.floor(damage/3.5)
end
local eplr = game.Players:GetPlayerFromCharacter(hit.Parent)
if eplr then
local ehum = eplr.Character:FindFirstChild("Humanoid")
ehum:TakeDamage(damage)
elseif not eplr then
local ehum = hit.Parent:FindFirstChild("Humanoid")
ehum:TakeDamage(damage)
end
table.insert(ht, hit.Parent.Name)
if not hit.Parent:GetAttribute("Blocking") then
local target = hit.Parent
print(module.Timer)
print('Before adding Timer: ', module.Timer)
if module.Timer[target] then -- checks if there is another rig/player in the stun module table
task.cancel(module.Timer[hit.Parent]) -- cancels the stun time thread
end
hit.Parent:SetAttribute("Stunned", true) -- stuns the player
print('After adding timer: ',module.Timer)
module.Timer[target] = task.delay(0.885, function() -- turns the stun into the stun table
if module.Timer[target] then -- sees if there is a plr/ rig in the table
hit.Parent:SetAttribute("Stunned", false) -- unstuns the plr/rig
module.Timer[target] = nil -- takes out the plr/ rig out of the table
table.remove(module.Timer, table.find(module.Timer, hit.Parent.Name))
print('Finishing the timer: ',module.Timer)
end
end)
end
end
end)
--[[hitBox.TouchEnded:Once(function(hit)
if hit.Name == "HumanoidRootPart" and hit.Parent.Name ~= plr.Name and hit.Parent:FindFirstChild("Humanoid") then
print("ended")
task.wait(1.5)
hit.Parent:SetAttribute("Stunned", false)
end
end)--]]
game.Debris:AddItem(hitBox, 0.7)
end
return module