How can i optimize this?

So, i’m trying to make the hitboxes of unanchored parts more bigger so they can be moved around by characters.

for _, part in workspace:GetDescendants() do
	if 
		part:IsA("BasePart") and not
		part:IsA("FileMesh") and
		part.Name ~= "Dummy" and
		part.Anchored == false
	then
		local BoundingBox = Instance.new("Part")
		BoundingBox.CFrame = part.CFrame
		BoundingBox.Transparency = 1
		BoundingBox.Size =  part.Size * 1.5
		BoundingBox.Name = HttpService:GenerateGUID()
		BoundingBox.Parent = part
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = part
		weld.Part1 = BoundingBox
		weld.Parent = part
	end
end

workspace.DescendantAdded:Connect(function(part)
	if 
		part:IsA("BasePart") and not
		part:IsA("FileMesh") and
		part.Name ~= "Dummy" and
		part.Anchored == false
	then
		local BoundingBox = Instance.new("Part")
		BoundingBox.CFrame = part.CFrame
		BoundingBox.Transparency = 1
		BoundingBox.Size =  part.Size * 1.5
		BoundingBox.Name = HttpService:GenerateGUID()
		BoundingBox.Parent = part
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = part
		weld.Part1 = BoundingBox
		weld.Parent = part
	end
end)

When i uncomment this code, and play the game using the normal roblox client, my camera is just stuck in the void, and my game does nothing.
However, if i comment out these codeblocks, and let it run, my game works fine.
Is this ethical? How can i optimiza this, and make it faster while also preventing memory leaks?

Note that this code is on the server.
Help will be appreciated.

2 Likes

The physics of roblox works in such a way that both parts having a collision that are in each other try to push off from themselves, which, when welding, causes the parts to instantly fly away into the void (far lands).

Why do you use such a solution? After all, all parts prematurely have all the necessary properties of hitboxes. You can just ignore some with ray or space calculations using FilterDescendantIntances, etc.

1 Like

Basically, i’m trying to expand the hitbox of every unanchored part, How can i do this without breaking physics?

You can disable the CanCollide and CanTouch properties of the created parts, leaving only CanQuery (participation in spatial calculations). This will be a quick option for you. CanTouch can be turned on, but be prepared that your productivity will drop by 2 times, or even more.

Hmm, i don’t think using spatial queries is good for expanding hitboxes. Any other methods i could try for hitbox expanding?

You could probably use Region3 (Region3.new()) that surrounds said part.

Region3s are a part of spatial queries (correct me if i’m wrong), i’m asking for a method without region3s or spatial queries.

Are you trying to get an effect like this?


If so, you can tag the unanchored parts using CollectionService (and this neat plugin to tag parts in studio) and loop through them to add a bounding box to it.

Example:

local CollectionService = game:GetService("CollectionService")

-- Run a loop to get every object that is tagged "Unanchored"
for _, Part in pairs(CollectionService:GetTagged("Unanchored")) do
	local BoundingBox = Instance.new("Part")
	BoundingBox.Name = "BoundingBox"
	BoundingBox.Size = Part.Size * 1.5
	BoundingBox.Transparency = 1
	BoundingBox.CFrame = Part.CFrame
	BoundingBox.Parent = Part
	local WeldConstraint = Instance.new("WeldConstraint")
	WeldConstraint.Parent = Part
	WeldConstraint.Part0 = Part
	WeldConstraint.Part1 = BoundingBox
	
	Part.CanCollide = false -- We don't need the other part's collision anymore because the BoundingBox is being used as it's hitbox
end

-- Run the same loop again when an instance is tagged "Unanchored" (except this time check if a BoundingBox already exists)
-- OR in the case an unanchored part is added by a script, you can get rid of this loop and just apply the boundingbox via that script
CollectionService:GetInstanceAddedSignal("Unanchored"):Connect(function()
	local UnanchoredBlocks = CollectionService:GetTagged("Unanchored")
	for _, Part in pairs(UnanchoredBlocks) do
		if Part:FindFirstChild("BoundingBox") ~= nil then
			continue
		else
			local BoundingBox = Instance.new("Part")
			BoundingBox.Name = "BoundingBox"
			BoundingBox.Size = Part.Size * 1.5
			BoundingBox.Transparency = 1
			BoundingBox.CFrame = Part.CFrame
			BoundingBox.Parent = Part
			local WeldConstraint = Instance.new("WeldConstraint")
			WeldConstraint.Parent = Part
			WeldConstraint.Part0 = Part
			WeldConstraint.Part1 = BoundingBox

			Part.CanCollide = false
		end
	end
end)

I don’t really know how to use collectionservice, does the tag editor correspond to CollectionService Tags?

This is a good solution. I saw you recommend the tag editor plugin. I’m not sure if you’re aware of this, but Roblox Studio now has a built-in Tag Editor, which I personally find a lot more user-friendly. You can find it right here:

Here’s a quick look at the built-in tag editor.

Yeah, it does. What the plugin does is basically call CollectionService:AddTag() and CollectionService:RemoveTag() on the objects you have selected.

1 Like

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