How would I create barriers that would only let through a certain team?

Hi! I’m trying to create different team spawns, and I want to create a barrier to prevent other teams from entering a specific team’s spawn.

This is the basic idea, except I’d make it transparent (only semi-transparent for the photo). Does anyone know how I’d do this? For reference, I’ll have around 12 or 13 different teams (it’s a really large game), and I’ll have to replicate this barrier for the rest of the team spawns.

Wouldn’t you do a touched event on the barrier, check if a player hit it, check the players team, then make it cancollide for them?

I recommend using PhysicsService to achieve this.
Read this guide to learn how collision groups work and how to make them.

Using PhysicsService, along with making a collision group per team, will allow you to achieve this effect.

Say, for example, you have 3 teams - red, blue, green.
By having 3 collisions group named “Red Team,” “Blue Team,” or “Red Team,” (you can name it however you’d like), you can assign the player’s character parts to that team. You will also need to assign the “barrier parts” to that team and make sure the collision settings are correct.

The script to assign players’ character parts is this:

local PhysicsService = game:GetService("PhysicsService")

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("HumanoidRootPart") -- wait just in case the character doesn't fully load
        for _, bodyPart in ipairs(character:GetDescendants()) do
            if bodyPart:IsA("BasePart") then
                PhysicsService:SetPartCollisionGroup(bodyPart, player.Team.Name) -- assuming the team name matches the collision group name
            end
        end
    end)
end)

The best way to do this is either Collision Groups or NoCollisionConstraints. If you don’t need the door to work perfectly, you can use the Touched event and the CanCollide property of parts/baseparts.

For the latter methods, connect the Player.CharacterAdded event to something that iterates through the baseparts and changes their collision group or adds NoCollisionConstraints(not recommended if you have multiple doors, if you have many teams you might need this, in which case you can add the constraints when the character touches the door.)

Edit: Also refer to great post above for details :+1:

1 Like

Since collision groups might be a bit confusing, this is how I setup the groups while assigning the barriers to their correlated group.