Hi Other Developers,
I wanted to check if two parts (Both are anchored with CanCollide off) are touching eachother, but don’t know how.
Thanks!
Hi Other Developers,
I wanted to check if two parts (Both are anchored with CanCollide off) are touching eachother, but don’t know how.
Thanks!
You can use :GetPartsInPart.
Code:
local partOne = workspace.PartOne
local partTwo = workspace.PartTwo
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {partTwo}
if #workspace:GetPartsInPart(partOne, overlapParams) > 0 then
print("Parts touching")
else
print("Not touching")
end
thank you, another question though:
is there any way to continuously check if the parts are touching? Like putting it into a loop or connecting a function to it?
Put it inside a heartbeat loop, I use heartbeats since I heard they are good for performance too, idk tho.
This is an extremely bad idea.
Heartbeat in general is okay for performance, if you absolutely must do something repeatedly.
Calling GetPartsInPart
inside a heartbeat function would decimate your performance.
works fine for me, replicates perfectly from client to server and multiplayer testing works flawless, on a normal mac m1.
You could use BasePart.Touched and BasePart.TouchEnded to change a variable if the touching part is the part you are looking for, and just read from that variable when you need to check if they are touching
Unfortunately, Touched
and TouchEnded
will only fire if either of the parts are unanchored.
It works, but it is horrible for performance, especially if you need to check multiple parts.
Ah right, sorry, I didn’t see that it says both parts are anchored.
In that case op can just run the function to check each time the two parts’ CFrame or Size property changes instead of every heartbeat
Performance is good heres a video (not only a constant partinpart check, but also a gettouchingparts for terrain, hehe
The video you sent does not show any kind of benchmarking or any statistics. That video only shows that your computer can run it on a single part at above 60 fps.
The system is heavier on the client than the server, client memory usage never surpasses 850 mb but I’ve not tested it on normal roblox only in studio so it probably varies but it’s stable and code is optimized to run well (except the parts where I do the part checks in a heartbeat), even works on Iphone x with 4 players in the game. But yeah Beans probably should search for a better solution.
It’s not really horrible for performance unless you do it with a lot of parts. But one or ten checks per stepped shouldn’t be noticeable.
No, it would actually perform extremely well, especially because of the OverlapParams I added.
For 100,000 iterations, it only takes 0.1 seconds. Think about how fast it would be for a reasonable amount of iterations like what you would do in a game.
Code:
local partOne = workspace.PartOne
local partTwo = workspace.PartTwo
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {partTwo}
local startingTime = os.clock()
for i = 1, 100000 do
if #workspace:GetPartsInPart(partOne, overlapParams) > 0 then end
end
print(`time elapsed: {os.clock() - startingTime}`) -- on average: 0.116311909999
Yes, you would do this:
local partOne = workspace.PartOne
local partTwo = workspace.PartTwo
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {partTwo}
while task.wait() do
if #workspace:GetPartsInPart(partOne, overlapParams) > 0 then
print("Parts touching")
else
print("Not touching")
end
end
Make sure to set up your variables outside the loop to maximize performance.
a quick question: how would you check if Mutiple parts are touching?
thanks.
Like if multiple parts are all touching each other at the same time?
Yes, i have no idea how to do that,
thanks.
Bit more tricky but you can do this:
local partOne = workspace.PartOne
local partsList = {
workspace.PartTwo,
workspace.PartThree,
workspace.PartFour
}
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {partsList}
while task.wait() do
if #workspace:GetPartsInPart(partOne, overlapParams) == #partsList then
print("Parts touching")
else
print("Not touching")
end
end