Hello, so I recently made a Region3 hitbox with a server script to detect player attacks and punches.
Here is the code:
game.Players.PlayerAdded:Connect(function(player)
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")
local hmndrootpart = Character:FindFirstChild("HumanoidRootPart")
player:WaitForChild("PlayerGui")
local regionSize = 6
local heightSize = 10
local visualizeRegion = Region3.new(Vector3.new(1,1,1), Vector3.new(regionSize,heightSize,regionSize))
local visualizePart = Instance.new("Part", game.Workspace.PlayerObjects)
visualizePart.Name = player.Name.."-visualizePart"
visualizePart.Anchored = true
visualizePart.Size = visualizeRegion.Size
visualizePart.CFrame = visualizeRegion.CFrame
visualizePart.Color = Color3.new(255,0,0)
visualizePart.Transparency = 1
visualizePart.CanCollide = false
game:GetService("RunService").Heartbeat:connect(function()
visualizePart.Position = hmndrootpart.Position
local PlayerSearchRegionTest = Region3.new(visualizePart.Position-(visualizePart.Size/2),visualizePart.Position+(visualizePart.Size/2))
local parts = workspace:FindPartsInRegion3WithWhiteList(PlayerSearchRegionTest, {workspace.CurrentCamera}, 1000)
for partIndex, part in pairs(parts) do
if part:FindFirstChild("Attack") or part.Name == "PunchHitbox" then
local dmgf = (part:FindFirstChild("StatMult").Value * 10) * part:FindFirstChild("AssetMult")
TakeDamage(dmgf,Humanoid,"Normal")
end
end
end)
end)
And here’s the punching code:
game.ReplicatedStorage.Remotes.PunchEvent.OnServerEvent:Connect(function(plr,punchcount,Mouse)
local Char = plr.Character or plr.CharacterAdded:Wait()
local Cooldown = false
local HitBoxPart
local Ready = true
local touchonlyonce = true
local hrp = Char.HumanoidRootPart
if punchcount == 5 then
local RL = Char["Left Leg"]
local Torso = Char.Torso
createTrail(Char["Left Leg"],Char.Torso,Trail,1)
end
local function HitBox()
HitBoxPart = Instance.new("Part",CombatEffectFolder)
HitBoxPart.CanCollide = false
HitBoxPart.Name = "PunchHitbox"
HitBoxPart.Size = Vector3.new(6, 6, 6)
HitBoxPart.Transparency = 1
HitBoxPart.Anchored = false
HitBoxPart.CFrame = Char.HumanoidRootPart.CFrame * CFrame.new(0,0,-1)
HitBoxPart.Massless = true
local weld = Instance.new("WeldConstraint")
weld.Part0 = HitBoxPart
weld.Part1 = Char.HumanoidRootPart
weld.Parent = HitBoxPart
Derbis:AddItem(HitBoxPart,0.1)
HitBoxPart.Touched:Connect(function(h)
if h.Parent:FindFirstChild('Humanoid') and h.Parent.Name ~= plr.Name and Cooldown == false and touchonlyonce == true then
local X = math.random(-180,180)
local Y = math.random(-180,180)
local Z = math.random(-180,180)
touchonlyonce = false
local Enemy = h.Parent.Humanoid
local HE = HitEffect:Clone()
HE.CFrame = h.Parent:FindFirstChild("HumanoidRootPart").CFrame * CFrame.Angles(X,math.rad(Y),Z)
HE.Parent = CombatEffectFolder
Derbis:AddItem(HE,0.1)
print(h)
if game.Players:FindFirstChild(h.Parent.Name) then
game.ReplicatedStorage.Remotes.HitClient:FireClient(game.Players:FindFirstChild(h.Parent.Name))
end
-- Cooldown = true
-- wait(1)
-- Cooldown = false
end
-- // ---------------
end)
end
HitBox()
end)
If you’d like a summary of the code:
The hitbox will make the player take damage only if: the part that touched the hitbox is called “PunchHitbox” or the part that touched it has a child that is called “Attack”
In the case of the punch, the punch hitbox part is called “PunchHitbox” so it should be detected but it’s not.
I don’t know if it’s because the Region3 can’t detect cancollide false parts or it’s something else, would appreciate help a lot.
Thank you for your time ![]()