Help with Region3

hello!
Im currently making a hitbox for my wandering trader NPC and I think I am going to do it with region3, but im not quite sure whats wrong, it is printing “1” but then its not printing anything else that it should be printing

edit: im completely new to region3 so please correct any mistakes I made

local Character = script.Parent
local tradingBool = Character.trading
local hitbox = Character.PlayerHitbox

local TopLeftFrontOfPart = hitbox.Position + Vector3.new(hitbox.Size.X/2,hitbox.Size.Y/2,hitbox.Size.Z/2)
local TopRightBackOfPart = hitbox.Position + (Vector3.new(hitbox.Size.X/2,hitbox.Size.Y/2,hitbox.Size.Z/2)*-1)

local region = Region3.new(TopLeftFrontOfPart,TopRightBackOfPart)

local getTouchingParts = game.Workspace:FindPartsInRegion3(region)

game:GetService("RunService").Stepped:Connect(function()
	print("1")
	for _,Part in pairs(game.Workspace:FindPartsInRegion3(region,nil,math.huge)) do
		print(Part.Name)
	end
end)

Try visualizing the region with a part, so you can see what’s really happening.

how would I go about doing this?

local part = Instance.new(“Part”)
part.CFrame = region.CFrame
part.Size = region.Size
part.Parent = game.Workspace

edit: why did you make a variable called getTouchingParts without using it?

not sure about the variable but anyways

I just tried that, and its generating the parts (I looked in the explorer), but I cant see them

local part = Instance.new("Part")
	part.CFrame = region.CFrame
	part.Size = region.Size
	part.Parent = game.Workspace
	part.Material = "Neon"
	part.Transparency = 0
	part.Anchored = true
	part.CanCollide = false

Is it inside the Stepped event?

yes

game:GetService("RunService").Stepped:Connect(function()
	print("1")
	
	local TopLeftFrontOfPart = hitbox.Position + Vector3.new(hitbox.Size.X/2,hitbox.Size.Y/2,hitbox.Size.Z/2)
	local TopRightBackOfPart = hitbox.Position + (Vector3.new(hitbox.Size.X/2,hitbox.Size.Y/2,hitbox.Size.Z/2)*-1)

	local region = Region3.new(TopLeftFrontOfPart,TopRightBackOfPart)
	
	local part = Instance.new("Part")
	part.CFrame = region.CFrame
	part.Size = region.Size
	part.Parent = game.Workspace
	part.Material = "Neon"
	part.Transparency = 0
	part.Anchored = true
	part.CanCollide = false
end)

EDIT: the part in trying to get the region from (the hitbox) is constantly moving if that matters

Now add this to your current code

for _,Part in pairs(region) do
print(Part.Name)
end

And tell me what it prints

it gave an error

Workspace.Jim.Trading_Script:22: invalid argument #1 to 'pairs' (table expected, got Region3)  

did you mean something else?

Sorry, I meant

for _,Part in pairs(workspace:FindPartsInRegion(region, nil, math.huge)) do
print(Part.Name)
end

its not printing anything, its not even running the loop

for _,Part in pairs(workspace:FindPartsInRegion3(region, nil, math.huge)) do
   	print("ran")
   	print(Part.Name)
   end

Could you show the whole script.

local Character = script.Parent
local tradingBool = Character.trading
local hitbox = Character.PlayerHitbox

game:GetService("RunService").Stepped:Connect(function()
	print("1")
	
	local TopLeftFrontOfPart = hitbox.Position + Vector3.new(hitbox.Size.X/2,hitbox.Size.Y/2,hitbox.Size.Z/2)
	local TopRightBackOfPart = hitbox.Position + (Vector3.new(hitbox.Size.X/2,hitbox.Size.Y/2,hitbox.Size.Z/2)*-1)

	local region = Region3.new(TopLeftFrontOfPart,TopRightBackOfPart)
	
	local part = Instance.new("Part")
	part.CFrame = region.CFrame
	part.Size = region.Size
	part.Parent = game.Workspace
	part.Material = "Neon"
	part.Transparency = 0
	part.Anchored = true
	part.CanCollide = false
	
	for _,Part in pairs(workspace:FindPartsInRegion3(region, nil, math.huge)) do
		print("ran")
		print(Part.Name)
	end
end)
1 Like

So is ‘1’ printing and could you show a screenshot of where the part(the visualizer) is.

thats the thing, I cant see the part

(link because the post is buggy and takes 20 minutes to upload an image)

So is that big box the part you made?

yeah, its what im using as a hitbox

I recommend you watching this tutorial: Advanced Roblox Scripting Tutorial #17 - Region3 (Beginner to Pro 2019) - YouTube
TimeStamp: 15:10

edit: he explains it very simple

ok this is the script I ended up with after quickly watching that part, it seems the loop just isnt running, should this be on the client or server side, and if it should be on the client side how would I do it on the server side

local Character = script.Parent
local tradingBool = Character.trading
local hitbox = Character.PlayerHitbox

local TopLeftFrontOfPart = hitbox.Position + Vector3.new(hitbox.Size.X/2,hitbox.Size.Y/2,hitbox.Size.Z/2)
local TopRightBackOfPart = hitbox.Position + (Vector3.new(hitbox.Size.X/2,hitbox.Size.Y/2,hitbox.Size.Z/2)*-1)

local region = Region3.new(TopLeftFrontOfPart,TopRightBackOfPart)


game:GetService("RunService").Stepped:Connect(function()
	print("1")
	local PNR = workspace:FindPartsInRegion3(region, nil, 10000)
	print("2")
	for i, part in pairs(PNR) do
		print("3")
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			print("woah")
		end
	end
end)

Try a while loop.

while true do
wait()
end

edit: you should also do it on the server