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:
local PhysicsService = game:GetService("PhysicsService") local blueDoors = "BlueDoors" local redDoors = "RedDoors" -- Create door collision groups PhysicsService:CreateCollisionGroup(blueDoors) PhysicsService:CreateCollisionGroup(redDoors) -- Add doors to their proper collision group PhysicsService:SetPartCollisionGroup(workspace.BlueDoor, blueDoors) PhysicsService:SetPartCollisionGroup(workspace.RedDoor, redDoors)
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:
-- Add doors to their proper collision group PhysicsService:SetPartCollisionGroup(workspace.BlueDoor, blueDoors) PhysicsService:SetPartCollisionGroup(workspace.RedDoor, redDoors) local bluePlayers = "BluePlayers" local redPlayers = "RedPlayers" -- Create player collision groups PhysicsService:CreateCollisionGroup(bluePlayers) PhysicsService:CreateCollisionGroup(redPlayers)
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:
-- Create player collision groups PhysicsService:CreateCollisionGroup(bluePlayers) PhysicsService:CreateCollisionGroup(redPlayers) local function setCollisionGroup(character, groupName) for _, child in ipairs(character:GetChildren()) do if child:IsA("BasePart") then PhysicsService:SetPartCollisionGroup(child, groupName) end end character.DescendantAdded:Connect(function(descendant) if descendant:IsA("BasePart") then PhysicsService:SetPartCollisionGroup(descendant, groupName) end end) end
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.
local Players = game:GetService("Players") local function onPlayerAdded(player) local function onCharacterAdded(character) setCollisionGroup(character, bluePlayers) end player.CharacterAdded:Connect(onCharacterAdded) end Players.PlayerAdded:Connect(onPlayerAdded)
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.
local function onPlayerAdded(player) local function onCharacterAdded(character) setCollisionGroup(character, bluePlayers) end player.CharacterAdded:Connect(onCharacterAdded) end Players.PlayerAdded:Connect(onPlayerAdded) PhysicsService:CollisionGroupSetCollidable(bluePlayers, blueDoors, false) PhysicsService:CollisionGroupSetCollidable(redPlayers, redDoors, false)