Region 3 simple hit detection

Hello, I am quite new and inexperienced with dealing with Region3's. This is my attempt to make a hitbox system. Is there anything bad or improvable in my script? Thank you!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, center)
	local region = Region3.new(center - Vector3.new(2,1,2), center + Vector3.new(2,1,2))
	local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region, {workspace.Baseplate, table.unpack(player.Character:GetChildren())}, 1)
	
	local heartbeat
	
	----------[ VISUALS ]---------
	local part = Instance.new("Part")
	part.CFrame = region.CFrame
	part.Size = region.Size
	part.Anchored = true
	part.CanCollide = false
	part.Transparency = 0.5
	part.Parent = workspace
	----------[ VISUALS ]---------
	
	heartbeat = RunService.Heartbeat:Connect(function()
		for i, bodyPart in pairs(partsInRegion) do
			local character = bodyPart.Parent
			
			if character:FindFirstChild("Humanoid") then
				print("character detected.")
				
				heartbeat:Disconnect()
			end
		end
	end)
end)

It’s looks fine and simple as is no need to over-simplify things.

1 Like

I know this topic was bumped so I know my suggestions below might not have existed, I’m just leaving my two cents here because someone else trying to create Region3 hit detection could stumble on this topic in the future.

My suggestions:

  • Convert workspace:FindPartsInRegion3WithIgnoreList to the new API.
  • I thought the script was longer, so I don’t actually have anymore suggestions, lol.

Only issue I could see is with accessory instances that have base-part children which would cause the hit to not be detected.

Apart from that; the code looks good.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")


local function getBodyParts(char)
	local bodyParts = {}
	
	for i, child in pairs(char:GetChildren()) do
		if child:IsA("BasePart") then
			table.insert(bodyParts, child)
		end
	end
	
	return bodyParts
end

local function getCharacters(parts)
	local characters = {}
	
	for i, bodyPart in pairs(parts) do
		local character = bodyPart.Parent
		
		if character:FindFirstChild("Humanoid") then
			table.insert(characters, bodyPart)
		end
	end
	
	return characters
end

ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, center)
	local region = Region3.new(center - Vector3.new(2,1,2), center + Vector3.new(2,1,2))
	local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region, {workspace.Baseplate, table.unpack(player.Character:GetChildren())}, 1)
	
	local heartbeat
	
	----------[ VISUALS ]---------
	local part = Instance.new("Part")
	part.CFrame = region.CFrame
	part.Size = region.Size
	part.Anchored = true
	part.CanCollide = false
	part.Transparency = 0.5
	part.Parent = workspace
	----------[ VISUALS ]---------
	
	heartbeat = RunService.Heartbeat:Connect(function()
		for i, bodyPart in pairs(partsInRegion) do
			local character = bodyPart.Parent
			
			if character:FindFirstChild("Humanoid") then
				print("character detected.")
				
				heartbeat:Disconnect()
			end
		end
	end)
end)