Trying to prevent "teammate tackling", code works but spams errors

I’m trying to prevent players from the same team to “tackle” eachother in a soccer game, the script works but spams the console with errors

I have re-checked the code multiple times yet i cant tell what’s causing the error, it errors when i try to tackle someone that is the same team as me.

My code: (The prints are just there for debugging purposes).

Player.Character["Right Leg"].Touched:connect(function(hit)
	if Kick == false then return end
	local plrhit = game.Players:GetPlayerFromCharacter(hit.Parent)	
	if plrhit.TeamColor == Player.TeamColor then
		print('Can\'t tackle teammates')
	else
		if hit.Parent:FindFirstChild("Humanoid") then
			game.ReplicatedStorage.Trip_Player:FireServer(hit.Parent)
			GameLogs:FireServer(hit.Parent.Name)
			Kick = false
		end
	end
	if hit.Name ~= "Ball" then return end
	local force = Player.Character["Right Leg"].CFrame.lookVector * 35
	local angle = Vector3.new(90000000000,100000,90000000000)
	Kick = false
	TM.ApplyForce(hit, angle, force, "Right Leg")
end)

Player.Character["Left Leg"].Touched:connect(function(hit)
	if Kick == false then return end
	local plrhit = game.Players:GetPlayerFromCharacter(hit.Parent)	
	if plrhit.TeamColor == Player.TeamColor then
		print('Can\'t tackle teammates')
	else
		if hit.Parent:FindFirstChild("Humanoid") then
			game.ReplicatedStorage.Trip_Player:FireServer(hit.Parent)
			GameLogs:FireServer(hit.Parent.Name)
			Kick = false
		end
	end
	if hit.Name ~= "Ball" then return end
	local force = Player.Character["Left Leg"].CFrame.lookVector * 35
	local angle = Vector3.new(90000000000,100000,90000000000)
	Kick = false
	TM.ApplyForce(hit, angle, force, "Left Leg")
end)

Lines 117 & 136: (These are the lines that error out)

	if plrhit.TeamColor == Player.TeamColor then
3 Likes

You need to make sure that the part touching the players left leg is part of a player.

if plrhit.Parent:FindFirstChild("Humanoid") then
    -- code here
end

Why don’t you just use .Team instead of TeamColor?

It’s more reliable anyways. It’s the same concept, just with the name of the team being asked here.

Fixed by making it:

	if plrhit and plrhit.TeamColor == Player.TeamColor then

instead of:

	if plrhit.TeamColor == Player.TeamColor then