How do I add an if statement to my script to check if the player is in a team

Alright, so I am doing team morphs, but one team has like 6 morphs and they are related to the ranks you have in the group. However all I want is to add an if statement that checks if the player is in the team “Mobile Task Force” if its true then he will wear the helmet. This is a normal script on a part that checks if it got touched, if it got touched players will automatically get the morph, but I want that only the team can get the morph when they touch it, so this is why I need an if statement that checks if the player is in the team “Mobile Task Force”.

Here the script:

     function onTouched(hit)
    	if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Helmet") == nil and  then
    		local g = script.Parent.Parent.Helmet:clone()
    		g.Parent = hit.Parent
    		local C = g:GetChildren()
    		for i=1, #C do
    			if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart" or C[i].className == "MeshPart"  then
    				local W = Instance.new("Weld")
    				W.Part0 = g.Middle
    				W.Part1 = C[i]
    				local CJ = CFrame.new(g.Middle.Position)
    				local C0 = g.Middle.CFrame:inverse()*CJ
    				local C1 = C[i].CFrame:inverse()*CJ
    				W.C0 = C0
    				W.C1 = C1
    				W.Parent = g.Middle
    			end
    				local Y = Instance.new("Weld")
    				Y.Part0 = hit.Parent.Head
    				Y.Part1 = g.Middle
    				Y.C0 = CFrame.new(0, 0, 0)
    				Y.Parent = Y.Part0
    		end

    		local h = g:GetChildren()
    		for i = 1, # h do
    			if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart" or C[i].className == "MeshPart"  then
    				h[i].Anchored = false
    				h[i].CanCollide = false
    			end
    		end
    		
    	end
    end

    script.Parent.Touched:connect(onTouched)

So first find the player from the touched part.

Then check if he’s on the team

local Team = game:GetService("Teams")["Mobile Task Force"]
if player.Team == Team then
-- do stuff
end

Not working, I tried that a lot of time but even if I do it it doesn’t morph me even if I am on the MTF team

You can just make the touch function with the team statement inside:

script.Parent.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChild("Humanoid") ~= nil then
      local char = hit.Parent
      local player = game.Players:GetPlayerFromCharacter(char)
      if player.Team == game.Teams["Mobile Task Force"] then
    		local g = script.Parent.Parent.Helmet:clone()
    		g.Parent = hit.Parent
    		local C = g:GetChildren()
    		for i=1, #C do
    			if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart" or C[i].className == "MeshPart"  then
    				local W = Instance.new("Weld")
    				W.Part0 = g.Middle
    				W.Part1 = C[i]
    				local CJ = CFrame.new(g.Middle.Position)
    				local C0 = g.Middle.CFrame:inverse()*CJ
    				local C1 = C[i].CFrame:inverse()*CJ
    				W.C0 = C0
    				W.C1 = C1
    				W.Parent = g.Middle
    			end
    				local Y = Instance.new("Weld")
    				Y.Part0 = hit.Parent.Head
    				Y.Part1 = g.Middle
    				Y.C0 = CFrame.new(0, 0, 0)
    				Y.Parent = Y.Part0
    		end

    		local h = g:GetChildren()
    		for i = 1, # h do
    			if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart" or C[i].className == "MeshPart"  then
    				h[i].Anchored = false
    				h[i].CanCollide = false
    		end
    	end
     end
   end
end)

This should work above, I have just copy and pasted the bit inside the if statement for the team so make sure that is correct as well.

First you gotta define the player who touched the part. So to do this, I usually run along the lines of

local char = hit.Parent
local humanoid = char:FindFirstChild'Humanoid'
if humanoid then
    local player = game.Players:FindFirstChild(char.Name)
end

Once you have the player you can do the code cooldaniel said.

Thank you very much, you helped me a lot!

1 Like