I am creating a melee tool hit detection system that does the following:
- Fire a raycast from the player’s head to their mouse position
- Send the player’s head position and mouse position (and other information relating to the raycast and hit detection) to the server
- If the raycast hits an enemy character, perform server-side checks to validate the hit and deal damage
- If the raycast returns nil, the server uses GetPartBoundsInBox to check an area in front of the player and deal damage to the first enemy found
Whenever I try to use GetPartBoundsInBox, nothing is being returned, and I don’t know how to physically see the area being checked – if anyone knows, please tell me.
I’m assuming the CFrame is incorrect, but I’m inexperienced with CFrames and don’t know how to correct it. Here’s the function.
local function performSweep(player, clientMousePos, clientHeadPos, clientHrp)
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist
overlapParams.FilterDescendantsInstances = {module:getEnemies(player)}
-- Here, clientHeadPos is the position of the player's head from the raycast
-- The range is the length of the raycast
-- I am trying to position the center of the box at the midpoint of the ray
local center = clientHeadPos + Vector3.new(0, -2.5, -module.CoreStats.Range / 2)
-- clientMousePos is passed in as Mouse.Hit.Position from the client (the game is locked in first person)
-- This tries to orient the box so it faces the player's mouse
local boxCenter = CFrame.lookAt(center, clientMousePos)
local boxSize = Vector3.new(4, 5, module.CoreStats.Range)
local sweepResult = workspace:GetPartBoundsInBox(boxCenter, boxSize, overlapParams)
if #sweepResult ~= 0 then
local firstHit = sweepResult[1]
local hitCharacter = firstHit:FindFirstAncestorWhichIsA("Model")
local hitPlayer = Players:GetPlayerFromCharacter(hitCharacter)
local hitHumanoid = hitCharacter:FindFirstChild("Humanoid")
if hitCharacter and hitPlayer and hitHumanoid then
module:dealDamage(player, clientHrp, hitPlayer, hitCharacter, hitHumanoid)
end
end
end