Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Finding specific part in an object array?

I've made a system where a player can place cubes. However, I'm now I'm trying to make a system that makes sure the cubes are placed within a certain region, and if not; it shouldn't get placed.

The region is supposed to cover anything over top of specific part (plot) and within its size.

This is what I got for this specific section of the code. (part is defined further up and is a 3x3 cube)

			local plot = game.Workspace.Bases:FindFirstChild(game.Players.LocalPlayer.Name)
			if part.Position.Y >= plot.Position.Y then
				
				local min = Vector3.new(plot.Position.X - plot.Size.X/2, plot.Position.Y, plot.Position.Z - plot.Size.Z/2)
				local max = Vector3.new(plot.Position.X + plot.Size.X/2, part.Position.Y, plot.Position.Z + plot.Size.Z/2)
				local region = Region3.new(min, max)
				local parts = workspace:FindPartsInRegion3(region)
				print(region)
				print(part.Position)
				if parts[part] == part then
				--And it would theoretically run the code here, there's obviously irrelevant code below this

However, it goes to my else statement every time which is set to just destroy the cube. The printing lines came out as

-37.5, 2.00000596, -469.5, 1, 0, 0, 0, 1, 0, 0, 0, 1; 120, 1.99998808, 120

15, 3, -492

The large span of numbers from the region makes me think maybe I used it wrong? I'm not sure where to go from here.

EDIT : I ran code like this

				local guide = Instance.new("Part")
				guide.Parent = game.Workspace
				guide.Anchored = true
				guide.Size = region.Size
				guide.CFrame = region.CFrame

to see the exact size and shape of the region. I then came to the conclusion, I had done the region right as it was exactly how it should be. This makes me even more confused however.

3 comments

0

You're not doing anything wrong, it's just printing the region and the part's position

0

also use vector3

0

but the script isn't working? it's going to else. Does it have to do for how I scanned for the part in the region?

1 answer

✓ Accepted answer

Try the example https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3 gave us, maybe it'll help.

local plot = game.Workspace.Bases:FindFirstChild(game.Players.LocalPlayer.Name)
if part.Position.Y >= plot.Position.Y then
    local min = Vector3.new(plot.Position.X - plot.Size.X / 2, plot.Position.Y, plot.Position.Z - plot.Size.Z / 2)
    local max = Vector3.new(plot.Position.X + plot.Size.X / 2, part.Position.Y, plot.Position.Z + plot.Size.Z / 2)
    local region = Region3.new(min, max)
    local parts = workspace:FindPartsInRegion3(region)
    print(region)
    print(part.Position)
    for i, v in pairs(game.Workspace:FindPartsInRegion3(region, nil, math.huge)) do
        --And it would theoretically run the code here, there's obviously irrelevant code below this
    end
end

2 comments

1

You're a legend

0

np

This discussion is preserved as it was. New answers, comments, and votes are closed.