What do you want to achieve?
I would like to check whether my structure is located in the designated region
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
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.
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 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.
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.
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
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?