Problems with Team Restricted Part

I want to make it so that a specific team can’t get into a place. This is a local script, I can’t understand why it doesn’t work.

local team = game.Teams.White

part.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	if plr and plr.Team == team then
		local Character = plr.Character

		script.Parent.CanCollide = true
	end
end)

Can anyone please help me? I know this might be simple, but all the devforum posts and tutorials about this all make a door open only for a few seconds and both teams can get in during those few seconds. I want one which doesn’t work like that.

put a local script in starter player scripts with this code
Capture

local team = game:GetService("Teams").White
local Part = Path to your door
local Player = game:GetService("Players").LocalPlayer

local function GetTeam()
   	if Player and Player.TeamColor == team.TeamColor then
        Part.CanCollide = false
     else
        Part.CanCollide = true
	end
end

while true do
  task.wait(1)
  GetTeam()
end

sorry if this isnt preformatted, on phone rn

local team = game.Teams.White

part.Touched:Connect(function(hit) — where are you getting part from
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if plr and plr.TeamColor == team.TeamColor then
	local Character = plr.Character
	script.Parent.CanCollide = false

script.Parent.Transparency = 1

end

end)

@4667hp Has it on the right track; however, the while loop isn’t needed for this application. Personally,

You can place it here,
931225065d091b1a9094835091ff0781

Personally, I wouldn’t place this in the server because players can exploit parts anywho on their client. Therefore, utilize a LocalScript.

Everytime the character is added, ALL scripts inside of ‘StarterCharacterScripts’ update with the new character; therefore, it’ll update with the Player.Team value attached to the Player in Players.

Why this is better than placing it in PlayerScripts: You do not need to constantly check the players team. StarterPlayerScripts ONLY update when the player is added, aka the very first time they join.

local Teams = game:GetService("Teams")

-- Place TEAM NAME, not color, inside of the table. Tables are great so you can just use table.find
local RestrictedTeams = {Teams["White"]}
local Part = script.Parent -- OR PATH TO DOOR


-- This will flat out only work if their team is NOT found in the table.
if not table.find(RestrictedTeams,Player.Team) then
      Part.Touched:Connect(function()
            -- Simple check to determine if the code needs to run.
            if Part.CanCollide == true then
                Part.CanCollide = false
                task.wait(1) -- needs a wait or door will just open & close instantly and cause some lag.
                Part.CanCollide = true
            end
      end)
end

I suggest using @4667hp’s code, but replace the quoted loop with the following, as it is more efficient:

Player:GetPropertyChangedSignal("Team"):Connect()
    GetTeam()
end)

I forgot that you could use get property changed signal on teams

Great point! More efficient to utilize GetPropertyChanged signal. However, I wonder if he is trying to do it on touched (less performance) or if he is trying to do it your way (updating and staying CanCollide = false for the duration of the team, More Performance)

Either way, it’s always best to suggest the more performance friendly alternative in my opinion. :smiley:

That if statement is only going to be ran once.

local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local Teams = Game:GetService("Teams")
local Wall = Workspace.Wall

local function OnTouched(Part)
	local Model = Part:FindFirstAncestorOfClass("Model")
	if not Model then return end
	local Player = Players:GetPlayerFromCharacter(Model)
	if not Player then return end
	Wall.CanCollide = not (Player.Team and (Player.Team == Teams.Team)) --Example team.
	task.wait(1)
	Wall.CanCollide = true --Make wall collidable.
end

Wall.Touched:Connect(OnTouched)