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
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
@4667hp Has it on the right track; however, the while loop isn’t needed for this application. Personally,
You can place it here,
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
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)
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)