Part in the middle of part

  1. What do you want to achieve?
    I would like to check whether my structure is located in the designated region

  2. What is the issue?
    I’m thinking about how to do it, but I can’t think of any easy way

code snippet

			for i, node in pairs(PlayersNodes:GetChildren()) do

						local clientStructure = StructuresFolder:FindFirstChild(structureButton.Name):Clone()
						clientStructure.BrickColor = BrickColor.new("Forest green")
						clientStructure.Material = Enum.Material.Neon
						clientStructure.CanCollide = false
						clientStructure.Parent = game.Workspace.StructuresInNodes
						print(clientStructure)

						RunService.RenderStepped:Connect(function()
							local mouseRay = mouse.UnitRay
							local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
							local ignoreList = {clientStructure, character}
							local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

							if hit and hit:IsA("Terrain") and node.Name == "NODE-"..player.Name and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlaceingDistance and character.Humanoid.Health > 0 then
									goodToPlace = true
									clientStructure.BrickColor = BrickColor.new("Forest green")

									if character.Humanoid.Health <= 0 then
										clientStructure:Destroy()
									end

								else
									goodToPlace = false
									clientStructure.BrickColor = BrickColor.new("Really red")
								end
  1. What solutions have you tried so far?
    Roblox dev forum

To check if your part is inside another part, you can use workspace:GetPartsInPart with an optional OverlapParams to help with filtering. A possible use:

--set up params so they will only detect your draggable part
local params: OverlapParams = OverlapParams.new()
params.FilterDescendantsInstances = {YourPart}
params.FilterType = Enum.RaycastFilterType.Include

local partsInPart: {BasePart} = workspace:GetPartsInPart(YourZone, params)

Alternative methods such as workspace:GetPartBoundsInBox might also be useful.

I’d also like to point out that workspace:FindPartOnRay, workspace:FindPartOnRayWithIgnoreList, and any other similar raycast methods are deprecated. You should use workspace:Raycast() with a RaycastParams object instead.

1 Like
workspace:GetPartsInPart

In my code it doesn’t work somehow

Could I see the code where you used it?

						RunService.RenderStepped:Connect(function()
							local mouseRay = mouse.UnitRay
							local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)

							local ignoreList = {clientStructure, character}
							local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

							if hit and node.Name == "NODE-"..player.Name and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlaceingDistance and character.Humanoid.Health > 0 then
								local params: OverlapParams = OverlapParams.new()
								params.FilterDescendantsInstances = {clientStructure}
								params.FilterType = Enum.RaycastFilterType.Include

								local partsInPart: {BasePart} = workspace:GetPartsInPart(node, params)
								
								if clientStructure.Position == partsInPart then
									goodToPlace = true
									clientStructure.BrickColor = BrickColor.new("Forest green")

									if character.Humanoid.Health <= 0 then
										clientStructure:Destroy()
									end

I’m trying to make clientStructure only be able to be placed in node

I don’t see anywhere in your code that node is being defined.

also change workspace:FindPartOnRayWithIgnoreList and the relevant code to the workspace:Raycast() method with RaycastParams, workspace:FindPartOnRayWithIgnoreList() is deprecated.

node is defined here

for i, node in pairs(PlayersNodes:GetChildren()) do

Oh, I didn’t notice you replaced your previous code, it wasn’t in your new snippet.

This will always equate to false, you’re comparing a Vector3 with a table of parts.

I know it’s bad because it doesn’t work, but I’m thinking how to make it work

You’re always comparing clientStructure.Position to a table of parts here. Even if you were to iterate over the array of parts and compare each one individually, the part would have to be exactly at the same position (at the centre of) the structure. You need to change that logic for your code to work.

If you give me some more information about what clientStructure is, I can help you more with fixing this. All I can tell right now is that it’s an instance cloned from ReplicatedStorage.

you still should change your raycast method

ClientStructures - contains all available structures, which will be used on the map, so I want to check if the clientStructures is in node so that the player cannot place this object anywhere else
image

Do you mean that you want to check if the two parts are intersecting? Or that you want to see if the player has already placed a structure of that type?

I would like it to work in such a way that when the clientStructure goes outside the node, it cannot be placed

Visualization

Change this to

if #partsInPart > 0 then

partsInPart is the number of parts inside the node, if it is 0 then it is outside.

You helped a lot, but now the only problem is how to check whether the mouse is in this zone

Oh right, sorry about that, scratch the previous solution and just check if the part is fully inside the node

With this, you have to create special lines for this, and I just want to check the square and check it without creating lines in it

GetPartsInPart probably doesn’t work becasue canquery is set to false. Try using a different spatial query method.

Try finding the correct solution on the devform. Here I’ve found another one for you to test: How to detect if a part is completely within another part? - #13 by Joshument