Are hitboxes WORTH optimising?

I’ve got a game with a lot of parts/meshes that will NEVER be touched by the player. Thing is, I’m using PreciseConvexDecomposition and I have read it lags the game a lot. Should I manually disable the hitboxes for meshes that WON’T be touched to increase performance or it isn’t really necessary?

You can make the part’s hitbox hull or box for the best performance. You can also turn off CanCollide, CanQuery, CastShadow, (if it barely displays a shadow or if you don’t need them) and CanTouch. Doing that will most likely increase FPS. Games usually contain a lot of parts, and if a lot of them are PreciseConvexDecomposition, your game’s FPS rate will definitely take a nosedive. You risk putting too much work on the Server, which can cause lag.

Thanks! unfortunately some meshes got really weird hitboxes due to some roblox error when importing. Should I still use hull in this case?

If you need a good hitbox, you can use GetPartBoundsInBox(). Here’s a code example.

local Part = script.Parent
local RunService = game:GetService("RunService")
local Connection = nil

Connection = RunService.Heartbeat:Connect(function(deltaTime)
	local Box = workspace:GetPartBoundsInBox(Part.CFrame, Part.Size)
	
	for _, v in Box do
		if v.Parent:IsA("Model") and v.Parent:FindFirstChild("Humanoid") then
			local Character = v.Parent
			local Humanoid = v.Parent:FindFirstChild("Humanoid")
			if Character and Humanoid then
				Humanoid:TakeDamage(20)
				-- And you can edit it further. You can make this code however you like. This is just an example of what "GetPartBoundsInBox" can do.
			end
		end
	end
end)

If you want the hitbox to ignore certain things, then here’s the following code for that as well.

local Part = script.Parent
local RunService = game:GetService("RunService")
local Connection = nil

local Filter = {Part} -- This table will contain all of the parts that the hitbox should ignore, such as itself for example, as you can see here.

local OverParams = OverlapParams.new()
OverParams.FilterType = Enum.RaycastFilterType.Exclude

Connection = RunService.Heartbeat:Connect(function(deltaTime)
	OverParams.FilterDescendantsInstances = Filter
	local Box = workspace:GetPartBoundsInBox(Part.CFrame, Part.Size, OverParams)
	
	for _, v in Box do
		if v:IsA("BasePart") and v.Name == "HumanoidRootPart" and v.Parent:IsA("Model") and v.Parent:FindFirstChild("Humanoid") then
			local RootPart = v
			local Character = v.Parent
			local Humanoid = v.Parent:FindFirstChild("Humanoid")
			if Character and Humanoid then
				table.insert(Filter, RootPart) -- Adds the HumanoidRootPart to the filter, so the hitbox will now ignore the player's HumanoidRootPart, even if it is inside of the hitbox.
				-- And you can edit it further. You can make this code however you like. This is just an example of what "GetPartBoundsInBox" can do.
			end
		end
	end
end)

Apologies for the late response. This took a long time to make.

2 Likes

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