Player from a red team loses life if he touches green team safe zone

Loose health if a player from red team touches green team safezone.

Hello there! I’m currently making a gun fighting game. But I wanted when a player from red team touch green team safe zone, the player from red team loose health.

If you can’t help me, I’d appreciate it anyway.

My script inside SafeZone part:


-- If a player from green team touches red team safezone part, decrease the health 
script.Parent.Touched:Connect(function(part)

game.Players.PlayerAdded:Connect(function(Player)  
	Player.CharacterAdded:Connect(function(Character)
		print(Player.TeamColor)
		if Player.TeamColor == BrickColor.new("Forest green") then
			Character.Humanoid.MaxHealth = 100
			Character.Humanoid.Health = 30
		end
	end)
 end)
end)


I looked all the forums and all the videos but none helped me.

Thanks.

1 Like

You will want to add an elseif statement to add another check.
I’ve revised your code a little bit aswell.

I’ve not tested it, so feel free to let me know how it goes.

local playerService = game:GetService("Players")

script.Parent.Touched:Connect(function(otherPart)
	local isCharacter = otherPart:FindFirstAncestorOfClass("Model")
	if isCharacter ~= nil then
		local isPlayer = playerService:GetPlayerFromCharacter(isCharacter)
		local humanFound = isCharacter:FindFirstChildOfClass("Humanoid")
	
		if isPlayer ~= nil and humanFound ~= nil then
			
			local teamColor = isPlayer.TeamColor
			if teamColor == BrickColor.new("Forest green") then
				--is green player touching safe zone
				humanFound.MaxHealth = 100
				humanFound.Health = 30
			else
				humanFound:TakeDamage(2)
			end
		end
	end
end)
2 Likes

~= nil is unnecessary since in Lua/Luau, everything that isn’t ‘nil’ or ‘false’ is considered truthy.

2 Likes

Oh, haha my bad. It’s a habit of mine