How I make a specific script work for only one team?

Hello, I am pretty new in the programming environment and I have a problem. I want to make a specific script work for only one team but I can’t do it.
I don’t know how to make it work, I tried with the if command but I can’t script it correctly. (The script that I use works).
I searched in the developer hub but I can’t find a solution.

Script without the working for one team part

local commands = {}

function Create(ClassName)
return function(Properties)
local Obj = Instance.new(ClassName)
for i,v in pairs(Properties) do
if type(i) == ‘number’ then
v.Parent = Obj
else
Obj[i] = v
end
end
return Obj
end
end
function HandleCharacter(Player, Character)
local Custom = Character:WaitForChild(‘Head’):clone()
Custom.Name = ‘TastiesOverhead’
Custom.Parent = Character
Custom.face:Destroy()
Character.Head.Transparency = 1
Create’Weld’{
Name = ‘CustomWeld’;
Parent = Custom;
Part0 = Character.Head;
Part1 = Custom;
}
Create’BillboardGui’{
Name = ‘Nametag’;
Parent = Custom;
Size = UDim2.new(5, 0, 0.5, 0);
StudsOffset = Vector3.new(0, 2, 0);
Create’TextLabel’{
Name = ‘NameLabel’;
BackgroundTransparency = 1;
Size = UDim2.new(1, 0, 1, 0);
Position = UDim2.new(0, 0, -0.8, 0);
Font = ‘ArialBold’;
Text = Character.Name;
TextColor3 = Color3.new(1, 1, 1);
TextScaled = true;
TextStrokeTransparency = 1;
};
Create’TextLabel’{
Name = ‘RankLabel’;
BackgroundTransparency = 1;
Size = UDim2.new(1, 0, 0.92, 0);
Position = UDim2.new(0, 0, 0.2, 0);
TextTransparency = .1;
Font = ‘SourceSansItalic’;
FontSize = Enum.FontSize.Size14;
Text = Player:GetRoleInGroup(4566728);
TextColor3 = Color3.new(1, 1, 1);
TextScaled = true;
TextStrokeTransparency = 1;
};
}

Custom.BrickColor = Character.Head.BrickColor
Character.Head.Changed:connect(function()
Character.Head.Transparency = 1
Custom.BrickColor = Character.Head.BrickColor
end)
Character.Head.Mesh.Changed:connect(function()
Custom:WaitForChild(‘Mesh’).MeshId = Character.Head:WaitForChild(‘Mesh’).MeshId
end)
end

local rainbow = function(callback)
local frequency = 0.1
local i = 0
while true do wait()
local red = math.sin(frequencyi + 0)127+128
local green = math.sin(frequency
i + 2
math.pi/3)127+128
local blue = math.sin(frequency
i + 4*math.pi/3)*127+128
callback(Color3.new(red/255,green/255,blue/255)) i = i+1
end end
function HandlePlayer(Player)
if Player.Character then
HandleCharacter(Player, Player.Character)
end
Player.CharacterAdded:connect(function(Character) HandleCharacter(Player, Character) end)
if Player.UserId == 0 or Player.UserId == 0 or Player.UserId == 0 or Player.UserId == 0 then
Player.CharacterAdded:connect(function(char) local label = char:WaitForChild(“TastiesOverhead”)
coroutine.resume(coroutine.create(rainbow),function(color)label.Nametag.RankLabel.TextColor3 = Color3.new(color.r,color.g,color.b)end)
end)
end
end
game.Players.PlayerAdded:connect(function(b)
HandlePlayer(b)
end)
for i,v in pairs(game.Players:GetPlayers()) do
HandlePlayer(v)
end

I am pretty new in the devforum too.

2 Likes

If the Teams are Static and you know which team you want to run that script, then you could use something like this, might need some changes in though.

local Player = game.Players.LocalPlayer
local Team = Player.Team

if Team.Name == "TeamName" then
 --Your script in here
end
6 Likes

You could do this:

local allowedteam = {"Police", "S.W.A.T"}

if allowedteam[plr.Team.Name] then

end
1 Like

This will not work as expected because your allowedteam table is in array form, therefore you’re performing constant lookups at nil indices and the script will never actually have any true conditions. There are two solutions for this: change allowedteam to a dictionary or use table.find and allow the if statement to pass if a truthy value is returned.

-- Dictionary method
local allowedteam = {
    ["Police"] = true,
    ["S.W.A.T"] = true,
}

if allowedteam[plr.Team.Name] then

-- Array method
local allowedteam = {"Police", "S.W.A.T"}

if table.find(allowedteam, plr.Team.Name) then
5 Likes

I tried it but my script does not recognize the plr.

Is this a localscript? or script

You would define the player and then run

if player.Team.Name == “” then
end

This should work

2 Likes

This is just example code, you aren’t meant to copy and paste it. Any issues occurring with this code sample are up to you to resolve, or you can salvage it as-is to help you in trying to create this system along with the responses that have been offered in this thread so far.

2 Likes

It is just a Normal Script not local.

To use the local player you need to use a local script, where is the script situated though? @AggelosGamer500

well is player defined as an object or string? if its an object you can add what i sent since it works off variables, if its a string value, you would have to find the player by eother getting players in pairs until found or from findfirstchild, i would recommend if its a string also adding something that would cancel out the request if it cant find the player.

2 Likes