Need help with :GetPartBoundsInBox()

I am creating a melee tool hit detection system that does the following:

  1. Fire a raycast from the player’s head to their mouse position
  2. Send the player’s head position and mouse position (and other information relating to the raycast and hit detection) to the server
  3. If the raycast hits an enemy character, perform server-side checks to validate the hit and deal damage
  4. 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
overlapParams.FilterDescendantsInstances = {module:getEnemies(player)}

might be this line, if module:getEnemies(player) returns an array then you should instead be writing

overlapParams.FilterDescendantsInstances = module:getEnemies(player)

That function does return an array, so thank you for correcting that. However, the box still isn’t returning any parts.

Never mind, that actually fixed it. I was playing around with the CFrame numbers before and made one of the numbers negative. Thank you.

1 Like