Ontouch for certain team

I tried to make a onTouched Event that if the player is on team (“20”)
The player cannot trigger the touch event. But if the player is on team (“10”), when he touches the part, it will trigger the onTouched event? Here is the script:

local DB = false
local Part = script.Parent
print("Script running")

Part.Touched:Connect(function(Hit)
	print("Event fired")
	local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
	if Player and not DB then
		DB = true
		print("Found a player")
		local Humanoid = Hit.Parent.Humanoid
		if Humanoid.Health == 0 then
			print("Making the part visible")
			for Loop = 1, 10 do
				Hit.Transparency -= 0.1
				wait(.1)
			end
			DB = false
		else
			print("The humanoid's health is not zero!")
		end
	end
end)


local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Arm = Player.Character:FindFirstChild("Left Arm2")
		if Arm then
			Arm.Transparency = 0.9
			wait(0.1)
			Arm.Transparency = 0.8
			wait(0.1)
			Arm.Transparency = 0.7
			wait(0.1)
			Arm.Transparency = 0.6
			wait(0.1)
			Arm.Transparency = 0.5
			wait(0.1)
			Arm.Transparency = 0.4
			wait(0.1)
			Arm.Transparency = 0.3
			wait(0.1)
			Arm.Transparency = 0.2
			wait(0.1)
			Arm.Transparency = 0.1
			wait(0.1)
			Arm.Transparency = 0
		end
    end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Arm2 = Player.Character:FindFirstChild("Right Arm2")
		if Arm2 then
			Arm2.Transparency = 0.9
			wait(0.1)
			Arm2.Transparency = 0.8
			wait(0.1)
			Arm2.Transparency = 0.7
			wait(0.1)
			Arm2.Transparency = 0.6
			wait(0.1)
			Arm2.Transparency = 0.5
			wait(0.1)
			Arm2.Transparency = 0.4
			wait(0.1)
			Arm2.Transparency = 0.3
			wait(0.1)
			Arm2.Transparency = 0.2
			wait(0.1)
			Arm2.Transparency = 0.1
			wait(0.1)
			Arm2.Transparency = 0
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Leg = Player.Character:FindFirstChild("Left Leg2")
		if Leg then
			wait(2.7)
			Leg.Transparency = 0.9
			wait(0.1)
			Leg.Transparency = 0.8
			wait(0.1)
			Leg.Transparency = 0.7
			wait(0.1)
			Leg.Transparency = 0.6
			wait(0.1)
			Leg.Transparency = 0.5
			wait(0.1)
			Leg.Transparency = 0.4
			wait(0.1)
			Leg.Transparency = 0.3
			wait(0.1)
			Leg.Transparency = 0.2
			wait(0.1)
			Leg.Transparency = 0.1
			wait(0.1)
			Leg.Transparency = 0
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Leg2 = Player.Character:FindFirstChild("Right Leg2")
		if Leg2 then
			wait(2.7)
			Leg2.Transparency = 0.9
			wait(0.1)
			Leg2.Transparency = 0.8
			wait(0.1)
			Leg2.Transparency = 0.7
			wait(0.1)
			Leg2.Transparency = 0.6
			wait(0.1)
			Leg2.Transparency = 0.5
			wait(0.1)
			Leg2.Transparency = 0.4
			wait(0.1)
			Leg2.Transparency = 0.3
			wait(0.1)
			Leg2.Transparency = 0.2
			wait(0.1)
			Leg2.Transparency = 0.1
			wait(0.1)
			Leg2.Transparency = 0 
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Torso = Player.Character:FindFirstChild("Torso2")
		if Torso then
			wait(2.7)
			Torso.Transparency = 0.9
			wait(0.1)
			Torso.Transparency = 0.8
			wait(0.1)
			Torso.Transparency = 0.7
			wait(0.1)
			Torso.Transparency = 0.6
			wait(0.1)
			Torso.Transparency = 0.5
			wait(0.1)
			Torso.Transparency = 0.4
			wait(0.1)
			Torso.Transparency = 0.3
			wait(0.1)
			Torso.Transparency = 0.2
			wait(0.1)
			Torso.Transparency = 0.1
			wait(0.1)
			Torso.Transparency = 0 
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local Head = Player.Character:FindFirstChild("HeadWhite")
		if Head then
			wait(3)
			Head.Transparency = 0.9
			wait(0.1)
			Head.Transparency = 0.8
			wait(0.1)
			Head.Transparency = 0.7
			wait(0.1)
			Head.Transparency = 0.6
			wait(0.1)
			Head.Transparency = 0.5
			wait(0.1)
			Head.Transparency = 0.4
			wait(0.1)
			Head.Transparency = 0.3
			wait(0.1)
			Head.Transparency = 0.2
			wait(0.1)
			Head.Transparency = 0.1
			wait(0.1)
			Head.Transparency = 0 
			Head:FindFirstChild("Decal").Transparency = 0
			
		end
	end
end)

local Part = script.Parent
Part.Touched:Connect(function(Hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
	if Player then
		local FirstHead = Player.Character.Head:FindFirstChild("face")
		if FirstHead then
			FirstHead.Transparency = 1
		end
	end
end)

First of all, this code needs to be refactored.

Second. From What I read, I couldn’t find anything checking for the player’s team.

To get this info you gotta get the TeamService. After that, add the statement if Player.Team == TeamService.20 then

There is a bunch of stuff you gotta check and fix:

You are making the same variable too many times, basically you are creating multiple local Part = script.Parent which confuses the script. You only need to do it once, at the start.

Next up, you dont need to do:

Arm.Transparency = 0.9
wait(0.1)
Arm.Transparency = 0.8
wait(0.1)
--repeat until 0

you can easily use a for i loop, this will save you multiple lines of code and its VERY useful.

for i = 1,0,-0.1 do --first number its where the loop starts, second is where it ends, and third, how much to add to i per loop. i starts as 1, then 0.9, and so on, until it reaches 0
      Arm.Transparency = i
      wait(0.1)
end

Next, you are connecting too many stuff to Part.Touched, what you should do it’s use the same loop to hide everything you need, and use only 1 connected function

--here put the first part of the code, and the variables u need
for i = 1,0,-0.1 do
      Arm1.Transparency = i
      Arm2.Transparency = i
      Leg1.Transparency = i
      Leg2.Transparency = i
      wait(0.1)
end

Finally, as @MrMoled said, there is no usage of a if Player.Team == TeamService.20 then if statement. Implement it to check if the player is in the correct team. You can easily do this after the part if Player and not DB then:

if Player.Team ~= TeamService["20"] then return end

Another way to do this is:

if Player.Team ~= game.Teams["20"] then return end

Hope this helps!

1 Like

Alright im gonna try this. Thanks

You’d have to do

if Player.Team ~= TeamService["20"] then return end

Since the team name starts with a number

Oh yeah i forgot, im gonna edit it, ty for the help!

1 Like