Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I detect if a player is inside a certain area?

In part of a game I'm working on I want to be able to detect if a player is inside a room or not so the camera position will change, and I'm stumped on how to do this. It's probably super simple and I'm just being stupid, but any responses would be super helpful. Thank you for reading this far.

2 comments

0

I believe you can use a region 3 for this

0

Read my post again, I made edits to answer your question

1 answer

✓ Accepted answer

For this kind of problem I recommend using Region3

You construct it with Region3.new(), and give a Vector3 min and max as the constructor parameters.

From there, you can use workspace:FindPartsInRegion3(), which takes in a region, an instance to ignore, and an int value of max parts (sort of similar to rays).

Check them out here

Also, the FindPartsInRegion3 method

Edit to answer your question:

Create a new table to store found players.

Iterate over the table that gets returned when calling the FindPartsInRegion3 method. For each part, check if its parent contains a humanoid; this means it is a player (likely).

From there, you can use the GetPlayerFromCharacter method from the player service (game:GetService("Players"):GetPlayerFromCharacter(char)) which takes a character as a parameter and returns the player - the character will be the parent of the part you're currently iterating over.

You can then check the table to see if the player already exists there; if it does, ignore the current part. Otherwise, add the new player to the table.

4 comments

0

Ok, but how would I find the specific player inside it?

0

Read my post again, I made edits to answer your question

0

Thank you! This was very helpful.

0

glad I could help :)

This discussion is preserved as it was. New answers, comments, and votes are closed.