Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I detect how far a part is from another part constantly?

Asked by 6 years ago

Basically I'm making a building system but I don't want players to be able to place parts near each other. (hard to explain here but that's the premise). My idea was to loop through all the parts that're placed, do some mathy stuff to see how close the part is, and if its closer than 10 studs it wouldn't allow the player. It seems very inefficient and possibly could cause lag if loads of parts are placed down. This is all happening from a LocalScript by the way. Anyone else have a better method of how I would do this?

1 answer

Log in to vote
0
Answered by
PlaasBoer 275 Moderation Voter
6 years ago
Edited 6 years ago

So what you do is minus the Vector3 of first part then the Vector3 of second part then use .magnitude to get the distance from them i think it is in studs.

Make a variable for part1 and part2

local distance = (part1.Position - part2.Position).magnitude
print(distance )

part1.Postion returns that parts Vector3

Read more http://wiki.roblox.com/index.php/Magnitude

0
But there isn't just 2 parts, it's all the placed parts. Let's just use 10 as an example. gmatchOnRoblox 103 — 6y
0
I can tell you what to do get all the parts distance from that part in loop then throught if statement check if the distance not greater than 10. if no distance is greater that 10 from the placed part then he can place it. PlaasBoer 275 — 6y
0
use a for loop. PlaasBoer 275 — 6y
View all comments (5 more)
0
Ohhhhh.. I don't know why I'm trying to do this constantly when I could just do it when the player tries to place down the part. Theennn loop through all the placed parts in workspace so it'll cause less-lag.. Okay then. Thanks for your help anyways PlaasBoer gmatchOnRoblox 103 — 6y
0
I would very much advise against traversing the entire workspace to find your desired parts. Instead you should use Region3, as in the video I provided: https://www.youtube.com/watch?v=_qJfWNi9Qf8&t=1s ScriptGuider 5640 — 6y
0
Alright, I'll try both methods out and see which works best. Region3 has always been a pain for me lol gmatchOnRoblox 103 — 6y
0
Region3 will work best, especially in performance. The speed of traversing the workspace opposed to using Region3 is inversely proportional to how many things are in your game. The more things, the worse the performance will be. Region3 is the best at handling this. ScriptGuider 5640 — 6y
0
Could you add me on Discord ScripterGuider? It'd be helpful if I have more questions about Region3 since I literally have no clue how to use it yet. Discord - Goat#2976 gmatchOnRoblox 103 — 6y
Ad

Answer this question