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

What is the best possible method for detecting a player in a specific radius of something?

Asked by 4 years ago
Edited 4 years ago

So I've taken up learning the ContextActionService. The best place to start seemed to be a door. While the script I have works fine, it feels inefficient. The idea of having to keep hundreds of doors stored in a folder and checking each individual one in a for loop seems like a bad solution to make this work. I was thinking of having the action be bound an unbound to open one specific door if the player came within a radius of it but I'm not entirely sure how to accomplish this.

Two methods I know would work are:

  1. An invisible part with a Touched event

  2. A while loop checking if the player is within a certain radius

Both options though seem very unappealing. Touched is a mess a lot of the time and I really don't like the idea of having a constant loop going that's constantly running checks given how common things like doors can be in maps. I've heard of using a Changed event but from what I've tested, Changed will not fire for player movement and neither will GetPropertyChangedSignal.

So what IS the best possible means of accomplishing something like this?

0
Have you tried Region3? You can make it like this: If player is in radius, print "Player in radius". You can check the information about Region3 on developer hub or youtube. zuup123 99 — 4y
0
Yes I have considered region3 but from the information I've gathered on it a simple magnitude check seems more efficient. XxTrueDemonxX 362 — 4y
0
Region3 is also quite slow - slower than just magnitude checks, at least. RiskoZoSlovenska 378 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

This is just a random idea but maybe it will work

You could have the client check if it's near any doors, and if it is, it sends a RemoteEvent to the server with the door it's near. The server then checks if the player really is near the door, and if it is, it opens it.

While there might be a delay between the player walking to the door and the door opening, in terms of computational power it's pretty efficient - loops are only run for one player on each client (no server strain unless a client spams the event, for which you can add a debounce or something). It shouldn't impact the client much, especially if you make it check for doors only once a while, something like once a second or so

Hope this helped :D

0
This is actually what I'm doing right now. I was a bit concerned it may be inefficient because I'm not entirely up to snuff when it comes to efficiency and all that. I didn't really consider the fact that the loop is really only running on the client itself. Thanks. XxTrueDemonxX 362 — 4y
0
no problem :D glad i could help RiskoZoSlovenska 378 — 4y
Ad

Answer this question