GetPartBoundsInBox is not returning objects when expected

My hitbox system hinges on the GetPartBoundsInBox function but it only works half of the time which is not very great for reasons you could guess.
The script itself is very early in it’s production and there is not too much things that could be causing this issue. The CFrame location is correct, The Size is correct, Everything seems to be firing correctly but for some reason it just sometimes does not want to work.

It not working → Forms - Roblox Studio (gyazo.com)

I checked if it had something to do with the CanCollide or CanQuiery properties but it does not seem to change the effect it has in any shape or form. I thought it could be the way it is getting fired but I dont notice anything in paticular in the way it’s fired being an issue otherwise the issue would be completly different.

I will send all relavent code here.

Client side script that fires an event parented to the character.

anim:GetMarkerReachedSignal("Melee"):Once(function(MeleeType:string)
						Remote:FireServer(true,"Melee",Mouse.Hit.Position,Heavy)
					end)

The server side script parented to the character that runs things from input. Litteraly one line that is relavent.

HitboxModule.RequestHitbox(SelfHum.RootPart.CFrame * CFrame.new(0,0,-2.5),Vector3.new(5,5,5),"Box",SelfChar,true)

The whole hitbox module I am using which seems to be most likely where the issue is.

local Functions = {}

----\/Services\/----

----/\Services/\----
----\/Local Functions\/----
local function RegisterImpact(HitPart:Instance)
	print("Hit "..HitPart.Name)
	local hithum = HitPart.Parent:FindFirstChildOfClass("Humanoid")
	if hithum then
		hithum:TakeDamage(5)
	end
end
----/\Local Functions/\----

----\/External Functions\/----
function Functions.RequestHitbox(HitboxCFrame:CFrame,HitboxSize:Vector3,HitboxType: "Box" | "Radius",Blacklist:{Instance} | Instance,Visualize:boolean)
	--print("Hitbox requested.")
	
	local function HitContacts(Contacts)
		for index = 1,#Contacts do
			local hit = Contacts[index]
			if hit then
				print("Contact "..tostring(hit))
				if type(Blacklist) == table then
					if table.find(Blacklist,hit) then
						return
					else
						RegisterImpact(hit)
					end
				elseif Blacklist:IsA("Instance") then
					if hit:IsDescendantOf(Blacklist) or hit == Blacklist then
						return
					else
						RegisterImpact(hit)
					end
				end
			end
		end
	end
	
	if HitboxType == "Box" then
		local Contacts = game.Workspace:GetPartBoundsInBox(HitboxCFrame,HitboxSize)
		HitContacts(Contacts)
		if Visualize == true then
			local Display = Instance.new("Part")
			Display.CFrame = HitboxCFrame
			Display.Size = HitboxSize
			Display.Shape = Enum.PartType.Block
			Display.Material = Enum.Material.ForceField
			Display.Anchored = true
			Display.CanCollide = false
			Display.CanTouch = false
			Display.CanQuery = false
			Display.Transparency = 0.5
			Display.Color = Color3.new(1, 0, 0)
			Display.Parent = workspace
			game.Debris:addItem(Display,1)
		end
	elseif HitboxType == "Radius" then
		local Contacts = game.Workspace:GetPartBoundsInRadius(HitboxCFrame.Position,HitboxSize.Magnitude/3)
		HitContacts(Contacts)
		if Visualize == true then
			local Display = Instance.new("Part")
			Display.Position = HitboxCFrame.Position
			Display.Size = HitboxSize
			Display.Shape = Enum.PartType.Ball
			Display.Material = Enum.Material.ForceField
			Display.Anchored = true
			Display.CanCollide = false
			Display.CanTouch = false
			Display.CanQuery = false
			Display.Transparency = 0.5
			Display.Color = Color3.new(1, 0, 0)
			Display.Parent = workspace
			game.Debris:addItem(Display,1)
		end
	end
end
----/\External Functions/\----


return Functions

Any kind of help would be nice. If you need more info then make sure to ask and I will be happy to send more parts of the code or some outputs in the print, ect.

Does this

print("Contact "..tostring(hit))

print you anything everytime you create a hitbox? As well as the hit print

Yes it does. When it does decide to work everything happens as expected.

Hmm, so here’s what i’m thinking. You’re using overlap params without specifying any ignored part, which means sometimes it’s very much possible the function is detecting parts from your character. However, these lines right here:

elseif Blacklist:IsA("Instance") then
   if hit:IsDescendantOf(Blacklist) or hit == Blacklist then
	   return
   else
	   RegisterImpact(hit)
   end

are not continuing the cycle but rather interrupting it completely. Replace your returns with a continue and see if anything changes. I do however recommend using OverlapParams to filter out your character. Might work better if instead you put, in case it’s an instance, the variable Blacklist in a table and then use it in the filtering.

local filter = {};
if type(Blacklist) == table then
	filter = Blacklist;
elseif Blacklist:IsA("Instance") then
	table.insert(filter, Blacklist)
end

local params = OverlapParams.new();
params.FilterType = Enum.RaycastFilterType.Include;
params..FilterDescendantsInstances = filter;

local Contacts = game.Workspace:GetPartBoundsInBox(HitboxCFrame,HitboxSize, params) -- will ignore your blacklisted instances

This would allow you to get rid of your Blacklist checks as well

Yep, that fixed everything! There is so many functions In Roblox that I simply don’t know about or forget exist that I wish I knew or remembered. Thanks very much for pointing this out.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.