Thank you for this extremely useful bit of information! “the holy grail” method is something I never even thought about using for finding players within a zone, I’ll be sure to use this a resource for future projects!
I’ve been looking for a good way to get players in a zone for a long time, thank you for this post!
This is actually a smart way to go about it. It even supports arbitrary space, which is a huge plus point when working with regions.
This thread dismisses clunky methods and mini-wars about the best way to do this (especially wrt magnitude), then brings the two best methods to the table and has them cooperate: the basis being the raycast and the Region3 being a gatekeeping supportive arm.
Of course, naturally, not all other methods should be dismissed (actually just the last 3) as they do have utility in some cases. Majority of cases seem to be simple area/room scans though so that should be covered well enough by this.
Thank you for sharing. This thread will go a long way.
so, the best way to keep finding players in zone is using loop?
while wait(1) do
for _,PlayersInZone in pairs(ZoneController:GetPlayersInZone(Workspace.Zone)) do
print(PlayerInZone)
end
end
Can you make a Demo place when they are in safe zone and giving them an ForceField and leaving safe zone removes ForceField.
For situations like the safe zone above, a loop works great, assuming you’re not checking too many times a second (a 0.5 second interval for example will be absolutely fine).
As a side-tip, you might want to check out the article colbert wrote on the While Wait-Do Idiom:
now this is epic. i’m most definitely going to use this in the future with my current project.
Interesting methodology! My own game uses GetTouchingParts
to determine region occupation and I must say the fact that I didn’t see this simple yet elegant solution is almost insulting to me. I’ll definitely be upgrading my private system to make use of this method.
Great post, this will work very well for general areas but it seems like it’d be rather tedious filling in boxes for more “natural” shapes/zones
Like have fun filling that with boxes
(Not to state the obvious)
Another solution for more complex shapes to check if a point is inside a polygon is to simply raycast
from anywhere outside the polygon to your point and count how often it hits a side of the polygon.
(Odd # hits = inside). This shouldn’t be very performance intensive at all and should satisfy most use cases
Very much appreciated for making this, since touched/untouched events are a mess. Thank you for this!
The method works the same for any type of shape. I believe you may be getting confused with the second image? Instead of creating lots of small boxes within the shape, we instead calculate a single Region3 determined upon the min and max bounds of the shape. Using this Region3, we then get all the players within the rough area, and fire a single ray below their HRP to confirm whether they are in the precise boundary we want to check for:
i.e. p3 and p4 will be returned
I meant making that shape out of parts is pretty tedious to do unless it’s hollow. But if it’s hollow firing a raycast downward won’t really be all that useful. I guess it wouldn’t be too tedious if you built its hollow shell and used some plugin to fill it in. I’m sure one exists
Great system. I tested it out by creating a simple weather system.
Maybe you could add a CheckIfPlayerInZone() function?
If the use case for this system isn’t key to gameplay, the ultimate solution would be to perform your downward raycast every frame on each of the clients for their respective characters, and when they hit a part identified as part of a zone, you can then fire a bindableevent and a remoteevent to cause stuff to happen both clientside and serverside. Clients could lie and fire the remoteevent when they aren’t actually in the zone, but for many applications, this isn’t important. For example, a zone that players must stand in if they want to participate in a minigame round, or a zone where stepping in it will apply weather effects (as seen in this thread).
If you want to further expand the utility of this module, you could add this clientside solution as a toggleable alternative, because for some requirements, the clientside method just makes more sense. For example, if someone wanted to open a shop UI upon the player entering a zone, it wouldn’t make sense to detect it on the server and then send a remoteevent signal to that player’s client.
Do you have any plans on adding Added/Leaving events? Would be neat to have!
Nice idea! This will work effectively if developed with the correct sanity checks.
Absolutely, the article focuses on retrieving multiple players server-side so I haven’t worried about this. Firing a downward ray every x amount of time and checking this against a dictionary of parts is almost all you’ll need to do for the client.
Having a module work effectively for both client and server is definitely something I’d like to do in the future.
What would you be looking to achieve with this?
Personally I’d use this feature for the following:
- Showing a ‘Entered
region_name
’ or ‘Leftregion_name
’ UI - Updating a live vote count on voting pads upon player enter/leave
- Initiating Cutscenes/camera movements, etc when a player enters a region
These things are currently a pain to do if you’re a part of circle gang (magnitude checks) or using .Touched.
This is amazing, saves so much time on this new game, I’m working on for a road name system .
Amazinngggg!!! Super useful for me, thank you!!