Hello! I am currently writing a punching/hitbox script for a combat system, and I cannot figure out why the target still gets hit, even while the hitbox is not currently being touched by it.
I have tried to look through different sources and tried to look into the math part of it, but nothing I could find online matched and it doesn’t seem to be wrong to me.
Here is the server sided script (hitbox):
local rs = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local events = rs:WaitForChild("Events")
local hitboxEvent = events:WaitForChild("Hitbox")
local kbEvent = events:WaitForChild("Knockback")
function newHitbox(character, size, offset, damage, linger)
local root = character:FindFirstChild("HumanoidRootPart") or character:FindFirstChild("Torso")
if root == nil then
print("root is nil")
return
end
local weld = Instance.new("WeldConstraint", root)
local hitbox = Instance.new("Part")
weld.Part0 = root
weld.Part1 = hitbox
hitbox.CanCollide = false
hitbox.CanQuery = false
hitbox.Massless = true
hitbox.Transparency = 0
hitbox.Size = size
hitbox.CFrame = root.CFrame + root.CFrame.LookVector * offset.X + Vector3.new(0, offset.Y)
hitbox.Parent = character
hitbox.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid == nil then
print("hit.Parent's humanoid is nil")
return
end
for _, v in pairs(hitbox:GetChildren()) do
if v:IsA("ObjectValue") then
if v.Value == hit.Parent then
kbEvent.OnServerEvent:Connect(function(targetCharacter)
local targetRoot = hit.Parent:FindFirstChild("HumanoidRootPart")
if targetRoot then
local direction = (targetRoot.Position - root.Position).Unit
local knockbackForce = 100
targetRoot.Velocity = direction * knockbackForce
end
end)
return
end
end
end
local hitCounter = Instance.new("ObjectValue", hitbox)
hitCounter.Value = hit.Parent
hit.Parent.Humanoid:TakeDamage(damage)
end)
game.Debris:AddItem(hitbox, linger)
end
hitboxEvent.OnServerEvent:Connect(function(plr, size, offset, damage, linger)
local character = plr.Character
if character then
newHitbox(character, size, offset, damage, linger)
end
end)
the client sided script pretty much just calls it during the animation.
Here is a video of what is going on:
Thank you!