In some games, it’s desirable to have doors or forcefields that certain players can pass through but others cannot. For instance, many team combat games will spawn players of each team in a safe area that other teams can’t access (so they aren’t attacked the moment they join or spawn).
There are various ways to accomplish this, but this tutorial utilizes
articles/Collision Filtering|collision filtering to create doors that only certain players can pass through.
Creating the Doors
For this simple example, the doors are basic block parts in the workspace named “BlueDoor” and “RedDoor.” Their material is
enum/Material|Glass and their
BasePart/Transparency|Transparency is set to 0.5.
Since these doors exist in the game world at the start, we can set up their collision groups immediately. In
ServerScriptService, create a
Script and paste the following code into it:
Now we need to add spawning players to an appropriate collision group. There are various ways to assign individual players to a group, including:
- Assign players a “color” based on their
articles/Player Spawns and Teams|teamcolor.
- Check if the player has earned a special
articles/Badges Special Game Awards|badgeor purchased a
articles/Game Passes One Time Purchases|game passwhich lets them access a restricted area.
For simplicity in this tutorial, we’ll just assign all incoming players to the “blue” team and give them access through the blue door, but not the red door.
As with the doors, let’s first set up player collision groups by adding these lines to the script:
When adding players to collision groups, it’s critical to remember that all parts of their character must be allowed to pass through the door — head, torso, arms, legs, feet, etc. We can do this with a function that finds all
BasePart objects, the base class of both
MeshPart|MeshParts which make up the player’s avatar:
Instance/DescendantAdded|DescendantAddedevent to detect when other parts are added. This ensures that all character parts receive the correct collision group assignment.
With this function in place, we can detect the
Players/PlayerAdded event which then listens for the
Player/CharacterAdded|CharacterAdded event and runs our
setCollisionGroup() function with the
bluePlayers collision group.
To finish up, we just need to define the collision groups that don’t collide with each other — specifically, blue players should not collide with blue doors, and red players should not collide with red doors.